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)  1985-2025, University of Amsterdam
    7                              VU University Amsterdam
    8                              CWI, Amsterdam
    9                              SWI-Prolog Solutions b.v.
   10    All rights reserved.
   11
   12    Redistribution and use in source and binary forms, with or without
   13    modification, are permitted provided that the following conditions
   14    are met:
   15
   16    1. Redistributions of source code must retain the above copyright
   17       notice, this list of conditions and the following disclaimer.
   18
   19    2. Redistributions in binary form must reproduce the above copyright
   20       notice, this list of conditions and the following disclaimer in
   21       the documentation and/or other materials provided with the
   22       distribution.
   23
   24    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   25    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   26    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   27    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   28    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   29    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   30    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   31    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   32    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   33    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   34    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   35    POSSIBILITY OF SUCH DAMAGE.
   36*/
   37
   38:- module('$syspreds',
   39          [ leash/1,
   40            visible/1,
   41            style_check/1,
   42            flag/3,
   43            atom_prefix/2,
   44            dwim_match/2,
   45            source_file_property/2,
   46            source_file/1,
   47            source_file/2,
   48            unload_file/1,
   49            exists_source/1,                    % +Spec
   50            exists_source/2,                    % +Spec, -Path
   51            prolog_load_context/2,
   52            stream_position_data/3,
   53            current_predicate/2,
   54            '$defined_predicate'/1,
   55            predicate_property/2,
   56            '$predicate_property'/2,
   57            (dynamic)/2,                        % :Predicates, +Options
   58            clause_property/2,
   59            current_module/1,                   % ?Module
   60            module_property/2,                  % ?Module, ?Property
   61            module/1,                           % +Module
   62            abolish_module/1,                   % +Module
   63            current_trie/1,                     % ?Trie
   64            trie_property/2,                    % ?Trie, ?Property
   65            working_directory/2,                % -OldDir, +NewDir
   66            shell/1,                            % +Command
   67            on_signal/3,
   68            current_signal/3,
   69            format/1,
   70            garbage_collect/0,
   71            set_prolog_stack/2,
   72            prolog_stack_property/2,
   73            absolute_file_name/2,
   74            tmp_file_stream/3,                  % +Enc, -File, -Stream
   75            call_with_depth_limit/3,            % :Goal, +Limit, -Result
   76            call_with_inference_limit/3,        % :Goal, +Limit, -Result
   77            rule/2,                             % :Head, -Rule
   78            rule/3,                             % :Head, -Rule, ?Ref
   79            numbervars/3,                       % +Term, +Start, -End
   80            term_string/3,                      % ?Term, ?String, +Options
   81            thread_create/2,                    % :Goal, -Id
   82            thread_join/1,                      % +Id
   83            sig_block/1,                        % :Pattern
   84            sig_unblock/1,                      % :Pattern
   85            transaction/1,                      % :Goal
   86            transaction/2,                      % :Goal, +Options
   87            transaction/3,                      % :Goal, :Constraint, +Mutex
   88            snapshot/1,                         % :Goal
   89            undo/1,                             % :Goal
   90            set_prolog_gc_thread/1,		% +Status
   91
   92            '$wrap_predicate'/5,                % :Head, +Name, -Closure, -Wrapped, +Body
   93            '$predicate_source_location'/2,     % :Head, -File:Line
   94            '$addr2line_location'/3             % +Description, -File, -Line
   95          ]).   96
   97:- meta_predicate
   98    dynamic(:, +),
   99    transaction(0),
  100    transaction(0,0,+),
  101    snapshot(0),
  102    rule(:, -),
  103    rule(:, -, ?),
  104    sig_block(:),
  105    sig_unblock(:).  106
  107
  108                /********************************
  109                *           DEBUGGER            *
  110                *********************************/
 map_bits(:Pred, +Modify, +OldBits, -NewBits)
  114:- meta_predicate
  115    map_bits(2, +, +, -).  116
  117map_bits(_, Var, _, _) :-
  118    var(Var),
  119    !,
  120    '$instantiation_error'(Var).
  121map_bits(_, [], Bits, Bits) :- !.
  122map_bits(Pred, [H|T], Old, New) :-
  123    map_bits(Pred, H, Old, New0),
  124    map_bits(Pred, T, New0, New).
  125map_bits(Pred, +Name, Old, New) :-     % set a bit
  126    !,
  127    bit(Pred, Name, Bits),
  128    !,
  129    New is Old \/ Bits.
  130map_bits(Pred, -Name, Old, New) :-     % clear a bit
  131    !,
  132    bit(Pred, Name, Bits),
  133    !,
  134    New is Old /\ (\Bits).
  135map_bits(Pred, ?(Name), Old, Old) :-   % ask a bit
  136    !,
  137    bit(Pred, Name, Bits),
  138    Old /\ Bits > 0.
  139map_bits(_, Term, _, _) :-
  140    '$type_error'('+|-|?(Flag)', Term).
  141
  142bit(Pred, Name, Bits) :-
  143    call(Pred, Name, Bits),
  144    !.
  145bit(_:Pred, Name, _) :-
  146    '$domain_error'(Pred, Name).
  147
  148:- public port_name/2.                  % used by library(test_cover)
  149
  150port_name(      call, 2'000000001).
  151port_name(      exit, 2'000000010).
  152port_name(      fail, 2'000000100).
  153port_name(      redo, 2'000001000).
  154port_name(     unify, 2'000010000).
  155port_name(     break, 2'000100000).
  156port_name(  cut_call, 2'001000000).
  157port_name(  cut_exit, 2'010000000).
  158port_name( exception, 2'100000000).
  159port_name(       cut, 2'011000000).
  160port_name(       all, 2'000111111).
  161port_name(      full, 2'000101111).
  162port_name(      half, 2'000101101).     % '
  163
  164leash(Ports) :-
  165    '$leash'(Old, Old),
  166    map_bits(port_name, Ports, Old, New),
  167    '$leash'(_, New).
  168
  169visible(Ports) :-
  170    '$visible'(Old, Old),
  171    map_bits(port_name, Ports, Old, New),
  172    '$visible'(_, New).
  173
  174style_name(atom,            0x0001) :-
  175    print_message(warning, decl_no_effect(style_check(atom))).
  176style_name(singleton,       0x0042).            % semantic and syntactic
  177style_name(discontiguous,   0x0008).
  178style_name(charset,         0x0020).
  179style_name(no_effect,       0x0080).
  180style_name(var_branches,    0x0100).
 style_check(+Spec) is nondet
  184style_check(Var) :-
  185    var(Var),
  186    !,
  187    '$instantiation_error'(Var).
  188style_check(?(Style)) :-
  189    !,
  190    (   var(Style)
  191    ->  enum_style_check(Style)
  192    ;   enum_style_check(Style)
  193    ->  true
  194    ).
  195style_check(Spec) :-
  196    '$style_check'(Old, Old),
  197    map_bits(style_name, Spec, Old, New),
  198    '$style_check'(_, New).
  199
  200enum_style_check(Style) :-
  201    '$style_check'(Bits, Bits),
  202    style_name(Style, Bit),
  203    Bit /\ Bits =\= 0.
 flag(+Name, -Old, +New) is det
True when Old is the current value associated with the flag Name and New has become the new value.
  211flag(Name, Old, New) :-
  212    Old == New,
  213    !,
  214    get_flag(Name, Old).
  215flag(Name, Old, New) :-
  216    with_mutex('$flag', update_flag(Name, Old, New)).
  217
  218update_flag(Name, Old, New) :-
  219    get_flag(Name, Old),
  220    (   atom(New)
  221    ->  set_flag(Name, New)
  222    ;   Value is New,
  223        set_flag(Name, Value)
  224    ).
  225
  226
  227                /********************************
  228                *             ATOMS             *
  229                *********************************/
  230
  231dwim_match(A1, A2) :-
  232    dwim_match(A1, A2, _).
  233
  234atom_prefix(Atom, Prefix) :-
  235    sub_atom(Atom, 0, _, _, Prefix).
  236
  237
  238                /********************************
  239                *             SOURCE            *
  240                *********************************/
 source_file(-File) is nondet
source_file(+File) is semidet
True if File is loaded into Prolog. If File is unbound it is bound to the canonical name for it. If File is bound it succeeds if the canonical name as defined by absolute_file_name/2 is known as a loaded filename.

Note that Time = 0 is used by PlDoc and other code that needs to create a file record without being interested in the time.

  253source_file(File) :-
  254    (   current_prolog_flag(access_level, user)
  255    ->  Level = user
  256    ;   true
  257    ),
  258    (   ground(File)
  259    ->  (   '$time_source_file'(File, Time, Level)
  260        ;   absolute_file_name(File, Abs),
  261            '$time_source_file'(Abs, Time, Level)
  262        ), !
  263    ;   '$time_source_file'(File, Time, Level)
  264    ),
  265    float(Time).
 source_file(+Head, -File) is semidet
source_file(?Head, ?File) is nondet
True when Head is a predicate owned by File.
  272:- meta_predicate source_file(:, ?).  273
  274source_file(M:Head, File) :-
  275    nonvar(M), nonvar(Head),
  276    !,
  277    (   '$c_current_predicate'(_, M:Head),
  278        predicate_property(M:Head, multifile)
  279    ->  multi_source_file(M:Head, File)
  280    ;   '$source_file'(M:Head, File)
  281    ).
  282source_file(M:Head, File) :-
  283    (   nonvar(File)
  284    ->  true
  285    ;   source_file(File)
  286    ),
  287    '$source_file_predicates'(File, Predicates),
  288    '$member'(M:Head, Predicates).
  289
  290multi_source_file(Head, File) :-
  291    State = state([]),
  292    nth_clause(Head, _, Clause),
  293    clause_property(Clause, source(File)),
  294    arg(1, State, Found),
  295    (   memberchk(File, Found)
  296    ->  fail
  297    ;   nb_linkarg(1, State, [File|Found])
  298    ).
 source_file_property(?File, ?Property) is nondet
True if Property is a property of the loaded source-file File.
  305source_file_property(File, P) :-
  306    nonvar(File),
  307    !,
  308    canonical_source_file(File, Path),
  309    property_source_file(P, Path).
  310source_file_property(File, P) :-
  311    property_source_file(P, File).
  312
  313property_source_file(modified(Time), File) :-
  314    '$time_source_file'(File, Time, user).
  315property_source_file(source(Source), File) :-
  316    (   '$source_file_property'(File, from_state, true)
  317    ->  Source = state
  318    ;   '$source_file_property'(File, resource, true)
  319    ->  Source = resource
  320    ;   Source = file
  321    ).
  322property_source_file(module(M), File) :-
  323    (   nonvar(M)
  324    ->  '$current_module'(M, File)
  325    ;   nonvar(File)
  326    ->  '$current_module'(ML, File),
  327        (   atom(ML)
  328        ->  M = ML
  329        ;   '$member'(M, ML)
  330        )
  331    ;   '$current_module'(M, File)
  332    ).
  333property_source_file(load_context(Module, Location, Options), File) :-
  334    clause(system:'$load_context_module'(File, Module, Options), true, Ref),
  335    '$time_source_file'(File, _, user),
  336    (   clause_property(Ref, file(FromFile)),
  337        clause_property(Ref, line_count(FromLine))
  338    ->  Location = FromFile:FromLine
  339    ;   Location = user
  340    ).
  341property_source_file(includes(Master, Stamp), File) :-
  342    system:'$included'(File, _Line, Master, Stamp).
  343property_source_file(included_in(Master, Line), File) :-
  344    system:'$included'(Master, Line, File, _).
  345property_source_file(derived_from(DerivedFrom, Stamp), File) :-
  346    system:'$derived_source'(File, DerivedFrom, Stamp).
  347property_source_file(reloading, File) :-
  348    source_file(File),
  349    '$source_file_property'(File, reloading, true).
  350property_source_file(load_count(Count), File) :-
  351    source_file(File),
  352    '$source_file_property'(File, load_count, Count).
  353property_source_file(number_of_clauses(Count), File) :-
  354    source_file(File),
  355    '$source_file_property'(File, number_of_clauses, Count).
  356property_source_file(loaded(Stamp), File) :-
  357    source_file(File),
  358    '$source_file_property'(File, loaded, Stamp).
 canonical_source_file(+Spec, -File) is semidet
File is the canonical representation of the source-file Spec.
  365canonical_source_file(Spec, File) :-
  366    atom(Spec),
  367    '$time_source_file'(Spec, _, _),
  368    !,
  369    File = Spec.
  370canonical_source_file(Spec, File) :-
  371    system:'$included'(_Master, _Line, Spec, _),
  372    !,
  373    File = Spec.
  374canonical_source_file(Spec, File) :-
  375    absolute_file_name(Spec, File,
  376                       [ file_type(source),
  377                         solutions(all),
  378                         file_errors(fail)
  379                       ]),
  380    source_file(File),
  381    !.
 exists_source(+Source) is semidet
 exists_source(+Source, -Path) 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.
  398exists_source(Source) :-
  399    exists_source(Source, _Path).
  400
  401exists_source(Source, Path) :-
  402    absolute_file_name(Source, Path,
  403                       [ file_type(prolog),
  404                         access(read),
  405                         file_errors(fail)
  406                       ]).
 prolog_load_context(+Key, -Value)
Provides context information for term_expansion and directives. Note that only the line-number info is valid for the '$stream_position'. Largely Quintus compatible.
  415prolog_load_context(module, Module) :-
  416    '$current_source_module'(Module).
  417prolog_load_context(file, File) :-
  418    input_file(File).
  419prolog_load_context(source, F) :-       % SICStus compatibility
  420    input_file(F0),
  421    '$input_context'(Context),
  422    '$top_file'(Context, F0, F).
  423prolog_load_context(stream, S) :-
  424    (   system:'$load_input'(_, S0)
  425    ->  S = S0
  426    ).
  427prolog_load_context(directory, D) :-
  428    input_file(F),
  429    file_directory_name(F, D).
  430prolog_load_context(dialect, D) :-
  431    current_prolog_flag(emulated_dialect, D).
  432prolog_load_context(term_position, TermPos) :-
  433    source_location(_, L),
  434    (   nb_current('$term_position', Pos),
  435        compound(Pos),              % actually set
  436        stream_position_data(line_count, Pos, L)
  437    ->  TermPos = Pos
  438    ;   TermPos = '$stream_position'(0,L,0,0)
  439    ).
  440prolog_load_context(script, Bool) :-
  441    (   '$toplevel':loaded_init_file(script, Path),
  442        input_file(File),
  443        same_file(File, Path)
  444    ->  Bool = true
  445    ;   Bool = false
  446    ).
  447prolog_load_context(variable_names, Bindings) :-
  448    (   nb_current('$variable_names', Bindings0)
  449    ->  Bindings = Bindings0
  450    ;   Bindings = []
  451    ).
  452prolog_load_context(term, Term) :-
  453    nb_current('$term', Term).
  454prolog_load_context(reloading, true) :-
  455    prolog_load_context(source, F),
  456    '$source_file_property'(F, reloading, true).
  457
  458input_file(File) :-
  459    (   system:'$load_input'(_, Stream)
  460    ->  stream_property(Stream, file_name(File))
  461    ),
  462    !.
  463input_file(File) :-
  464    source_location(File, _).
 unload_file(+File) is det
Remove all traces of loading file. If the file is a module file and used use_foreign_library/1,2 to load foreign extensions, these are removed first.
To be done
- Should we introduce a counterpart for initialization/1 that is called when a file is unloaded?
  476:- dynamic system:'$resolved_source_path'/2.  477
  478unload_file(File) :-
  479    (   canonical_source_file(File, Path)
  480    ->  unload_file_(Path),
  481        '$clear_source_admin'(Path),
  482        garbage_collect_clauses
  483    ;   true
  484    ).
  485
  486:- if(current_prolog_flag(open_shared_object, true)).  487unload_file_(Path) :-
  488    source_file_property(Path, module(M)),
  489    ensure_shlib,
  490    !,
  491    forall(shlib:foreign_library_property(Foreign, module(M)),
  492           shlib:unload_foreign_library(Foreign)),
  493    '$unload_file'(Path).
  494:- endif.  495unload_file_(Path) :-
  496    '$unload_file'(Path).
  497
  498:- if(current_prolog_flag(open_shared_object, true)).  499
  500		 /*******************************
  501		 *      FOREIGN LIBRARIES	*
  502		 *******************************/
 use_foreign_library(+FileSpec) is det
 use_foreign_library(+FileSpec, +Entry:atom) is det
Load and install a foreign library as load_foreign_library/1,2 and register the installation using initialization/2 with the option now. This is similar to using:
:- initialization(load_foreign_library(foreign(mylib))).

but using the initialization/1 wrapper causes the library to be loaded after loading of the file in which it appears is completed, while use_foreign_library/1 loads the library immediately. I.e. the difference is only relevant if the remainder of the file uses functionality of the C-library.

  521:- meta_predicate
  522    use_foreign_library(:),
  523    use_foreign_library(:, +).  524:- public
  525    use_foreign_library_noi/1.  526
  527use_foreign_library(FileSpec) :-
  528    ensure_shlib,
  529    initialization(use_foreign_library_noi(FileSpec), now).
  530
  531% noi -> no initialize; used by '$autoload':exports/3.
  532use_foreign_library_noi(FileSpec) :-
  533    ensure_shlib,
  534    shlib:load_foreign_library(FileSpec).
  535
  536use_foreign_library(FileSpec, Options) :-
  537    ensure_shlib,
  538    initialization(shlib:load_foreign_library(FileSpec, Options), now).
  539
  540ensure_shlib :-
  541    '$get_predicate_attribute'(shlib:load_foreign_library(_), defined, 1),
  542    '$get_predicate_attribute'(shlib:load_foreign_library(_,_), defined, 1),
  543    !.
  544ensure_shlib :-
  545    use_module(library(shlib), []).
  546
  547:- export(use_foreign_library/1).  548:- export(use_foreign_library/2).  549
  550:- elif(current_predicate('$activate_static_extension'/1)).  551
  552% Version when using shared objects is disabled and extensions are added
  553% as static libraries.
  554
  555:- meta_predicate
  556    use_foreign_library(:).  557:- public
  558    use_foreign_library_noi/1.  559:- dynamic
  560    loading/1,
  561    foreign_predicate/2.  562
  563use_foreign_library(FileSpec) :-
  564    initialization(use_foreign_library_noi(FileSpec), now).
  565
  566use_foreign_library_noi(Module:foreign(Extension)) :-
  567    setup_call_cleanup(
  568        asserta(loading(foreign(Extension)), Ref),
  569        @('$activate_static_extension'(Extension), Module),
  570        erase(Ref)).
  571
  572:- export(use_foreign_library/1).  573
  574system:'$foreign_registered'(M, H) :-
  575    (   loading(Lib)
  576    ->  true
  577    ;   Lib = '<spontaneous>'
  578    ),
  579    assert(foreign_predicate(Lib, M:H)).
 current_foreign_library(?File, -Public)
Query currently loaded shared libraries.
  585current_foreign_library(File, Public) :-
  586    setof(Pred, foreign_predicate(File, Pred), Public).
  587
  588:- export(current_foreign_library/2).  589
  590:- endif. /* open_shared_object support */
  591
  592                 /*******************************
  593                 *            STREAMS           *
  594                 *******************************/
 stream_position_data(?Field, +Pos, ?Date)
Extract values from stream position objects. '$stream_position' is of the format '$stream_position'(Byte, Char, Line, LinePos)
  601stream_position_data(Prop, Term, Value) :-
  602    nonvar(Prop),
  603    !,
  604    (   stream_position_field(Prop, Pos)
  605    ->  arg(Pos, Term, Value)
  606    ;   throw(error(domain_error(stream_position_data, Prop)))
  607    ).
  608stream_position_data(Prop, Term, Value) :-
  609    stream_position_field(Prop, Pos),
  610    arg(Pos, Term, Value).
  611
  612stream_position_field(char_count,    1).
  613stream_position_field(line_count,    2).
  614stream_position_field(line_position, 3).
  615stream_position_field(byte_count,    4).
  616
  617
  618                 /*******************************
  619                 *            CONTROL           *
  620                 *******************************/
 call_with_depth_limit(:Goal, +DepthLimit, -Result)
Try to proof Goal, but fail on any branch exceeding the indicated depth-limit. Unify Result with the maximum-reached limit on success, depth_limit_exceeded if the limit was exceeded and fails otherwise.
  628:- meta_predicate
  629    call_with_depth_limit(0, +, -).  630
  631call_with_depth_limit(G, Limit, Result) :-
  632    '$depth_limit'(Limit, OLimit, OReached),
  633    (   catch(G, E, '$depth_limit_except'(OLimit, OReached, E)),
  634        '$depth_limit_true'(Limit, OLimit, OReached, Result, Det),
  635        ( Det == ! -> ! ; true )
  636    ;   '$depth_limit_false'(OLimit, OReached, Result)
  637    ).
 call_with_inference_limit(:Goal, +InferenceLimit, -Result)
Equivalent to call(Goal), but poses a limit on the number of inferences. If this limit is reached, Result is unified with inference_limit_exceeded, otherwise Result is unified with ! if Goal succeeded without a choicepoint and true otherwise.

Note that we perform calls in system to avoid auto-importing, which makes raiseInferenceLimitException() fail to recognise that the exception happens in the overhead.

  650:- meta_predicate
  651    call_with_inference_limit(0, +, -).  652
  653call_with_inference_limit(G, Limit, Result) :-
  654    '$inference_limit'(Limit, OLimit),
  655    (   catch(G, Except,
  656              system:'$inference_limit_except'(OLimit, Except, Result0)),
  657        (   Result0 == inference_limit_exceeded
  658        ->  !
  659        ;   system:'$inference_limit_true'(Limit, OLimit, Result0),
  660            ( Result0 == ! -> ! ; true )
  661        ),
  662        Result = Result0
  663    ;   system:'$inference_limit_false'(OLimit)
  664    ).
  665
  666
  667                /********************************
  668                *           DATA BASE           *
  669                *********************************/
  670
  671/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  672The predicate current_predicate/2 is   a  difficult subject since  the
  673introduction  of defaulting     modules   and   dynamic     libraries.
  674current_predicate/2 is normally  called with instantiated arguments to
  675verify some  predicate can   be called without trapping   an undefined
  676predicate.  In this case we must  perform the search algorithm used by
  677the prolog system itself.
  678
  679If the pattern is not fully specified, we only generate the predicates
  680actually available in this  module.   This seems the best for listing,
  681etc.
  682- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  683
  684
  685:- meta_predicate
  686    current_predicate(?, :),
  687    '$defined_predicate'(:).  688
  689current_predicate(Name, Module:Head) :-
  690    (var(Module) ; var(Head)),
  691    !,
  692    generate_current_predicate(Name, Module, Head).
  693current_predicate(Name, Term) :-
  694    '$c_current_predicate'(Name, Term),
  695    '$defined_predicate'(Term),
  696    !.
  697current_predicate(Name, Module:Head) :-
  698    default_module(Module, DefModule),
  699    '$c_current_predicate'(Name, DefModule:Head),
  700    '$defined_predicate'(DefModule:Head),
  701    !.
  702current_predicate(Name, Module:Head) :-
  703    '$autoload':autoload_in(Module, general),
  704    \+ current_prolog_flag(Module:unknown, fail),
  705    (   compound(Head)
  706    ->  compound_name_arity(Head, Name, Arity)
  707    ;   Name = Head, Arity = 0
  708    ),
  709    '$find_library'(Module, Name, Arity, _LoadModule, _Library),
  710    !.
  711
  712generate_current_predicate(Name, Module, Head) :-
  713    current_module(Module),
  714    QHead = Module:Head,
  715    '$c_current_predicate'(Name, QHead),
  716    '$get_predicate_attribute'(QHead, defined, 1).
  717
  718'$defined_predicate'(Head) :-
  719    '$get_predicate_attribute'(Head, defined, 1),
  720    !.
 predicate_property(?Predicate, ?Property) is nondet
True when Property is a property of Predicate.
  726:- meta_predicate
  727    predicate_property(:, ?).  728
  729:- multifile
  730    '$predicate_property'/2.  731
  732:- '$iso'(predicate_property/2).  733
  734predicate_property(Pred, Property) :-           % Mode ?,+
  735    nonvar(Property),
  736    !,
  737    property_predicate(Property, Pred).
  738predicate_property(Pred, Property) :-           % Mode +,-
  739    define_or_generate(Pred),
  740    '$predicate_property'(Property, Pred).
 property_predicate(+Property, ?Pred)
First handle the special cases that are not about querying normally defined predicates: undefined, visible and autoload, followed by the generic case.
  748property_predicate(undefined, Pred) :-
  749    !,
  750    Pred = Module:Head,
  751    current_module(Module),
  752    '$c_current_predicate'(_, Pred),
  753    \+ '$defined_predicate'(Pred),          % Speed up a bit
  754    \+ current_predicate(_, Pred),
  755    goal_name_arity(Head, Name, Arity),
  756    \+ system_undefined(Module:Name/Arity).
  757property_predicate(visible, Pred) :-
  758    !,
  759    visible_predicate(Pred).
  760property_predicate(autoload(File), Head) :-
  761    !,
  762    \+ current_prolog_flag(autoload, false),
  763    '$autoload':autoloadable(Head, File).
  764property_predicate(implementation_module(IM), M:Head) :-
  765    !,
  766    atom(M),
  767    (   default_module(M, DM),
  768        '$get_predicate_attribute'(DM:Head, defined, 1)
  769    ->  (   '$get_predicate_attribute'(DM:Head, imported, ImportM)
  770        ->  IM = ImportM
  771        ;   IM = M
  772        )
  773    ;   \+ current_prolog_flag(M:unknown, fail),
  774        goal_name_arity(Head, Name, Arity),
  775        '$find_library'(_, Name, Arity, LoadModule, _File)
  776    ->  IM = LoadModule
  777    ;   M = IM
  778    ).
  779property_predicate(iso, _:Head) :-
  780    callable(Head),
  781    !,
  782    goal_name_arity(Head, Name, Arity),
  783    current_predicate(system:Name/Arity),
  784    '$predicate_property'(iso, system:Head).
  785property_predicate(built_in, Module:Head) :-
  786    callable(Head),
  787    !,
  788    goal_name_arity(Head, Name, Arity),
  789    current_predicate(Module:Name/Arity),
  790    '$predicate_property'(built_in, Module:Head).
  791property_predicate(Property, Pred) :-
  792    define_or_generate(Pred),
  793    '$predicate_property'(Property, Pred).
  794
  795goal_name_arity(Head, Name, Arity) :-
  796    compound(Head),
  797    !,
  798    compound_name_arity(Head, Name, Arity).
  799goal_name_arity(Head, Head, 0).
 define_or_generate(+Head) is semidet
define_or_generate(-Head) is nondet
If the predicate is known, try to resolve it. Otherwise generate the known predicate, but do not try to (auto)load the predicate.
  808define_or_generate(M:Head) :-
  809    callable(Head),
  810    atom(M),
  811    '$get_predicate_attribute'(M:Head, defined, 1),
  812    !.
  813define_or_generate(M:Head) :-
  814    callable(Head),
  815    nonvar(M), M \== system,
  816    !,
  817    '$define_predicate'(M:Head).
  818define_or_generate(Pred) :-
  819    current_predicate(_, Pred),
  820    '$define_predicate'(Pred).
  821
  822
  823'$predicate_property'(interpreted, Pred) :-
  824    '$get_predicate_attribute'(Pred, foreign, 0).
  825'$predicate_property'(visible, Pred) :-
  826    '$get_predicate_attribute'(Pred, defined, 1).
  827'$predicate_property'(built_in, Pred) :-
  828    '$get_predicate_attribute'(Pred, system, 1).
  829'$predicate_property'(exported, Pred) :-
  830    '$get_predicate_attribute'(Pred, exported, 1).
  831'$predicate_property'(public, Pred) :-
  832    '$get_predicate_attribute'(Pred, public, 1).
  833'$predicate_property'(non_terminal, Pred) :-
  834    '$get_predicate_attribute'(Pred, non_terminal, 1).
  835'$predicate_property'(foreign, Pred) :-
  836    '$get_predicate_attribute'(Pred, foreign, 1).
  837'$predicate_property'((dynamic), Pred) :-
  838    '$get_predicate_attribute'(Pred, (dynamic), 1).
  839'$predicate_property'((static), Pred) :-
  840    '$get_predicate_attribute'(Pred, (dynamic), 0).
  841'$predicate_property'((volatile), Pred) :-
  842    '$get_predicate_attribute'(Pred, (volatile), 1).
  843'$predicate_property'((thread_local), Pred) :-
  844    '$get_predicate_attribute'(Pred, (thread_local), 1).
  845'$predicate_property'((multifile), Pred) :-
  846    '$get_predicate_attribute'(Pred, (multifile), 1).
  847'$predicate_property'((discontiguous), Pred) :-
  848    '$get_predicate_attribute'(Pred, (discontiguous), 1).
  849'$predicate_property'(imported_from(Module), Pred) :-
  850    '$get_predicate_attribute'(Pred, imported, Module).
  851'$predicate_property'(transparent, Pred) :-
  852    '$get_predicate_attribute'(Pred, transparent, 1).
  853'$predicate_property'(meta_predicate(Pattern), Pred) :-
  854    '$get_predicate_attribute'(Pred, transparent, 1),
  855    '$get_predicate_attribute'(Pred, meta_predicate, Pattern).
  856'$predicate_property'(mode(Pattern), Pred) :-
  857    '$get_predicate_attribute'(Pred, transparent, 0),
  858    '$get_predicate_attribute'(Pred, meta_predicate, Pattern).
  859'$predicate_property'(file(File), Pred) :-
  860    '$get_predicate_attribute'(Pred, file, File).
  861'$predicate_property'(line_count(LineNumber), Pred) :-
  862    '$get_predicate_attribute'(Pred, line_count, LineNumber).
  863'$predicate_property'(notrace, Pred) :-
  864    '$get_predicate_attribute'(Pred, trace, 0).
  865'$predicate_property'(nodebug, Pred) :-
  866    '$get_predicate_attribute'(Pred, hide_childs, 1).
  867'$predicate_property'(spying, Pred) :-
  868    '$get_predicate_attribute'(Pred, spy, 1).
  869'$predicate_property'(number_of_clauses(N), Pred) :-
  870    '$get_predicate_attribute'(Pred, number_of_clauses, N).
  871'$predicate_property'(number_of_rules(N), Pred) :-
  872    '$get_predicate_attribute'(Pred, number_of_rules, N).
  873'$predicate_property'(last_modified_generation(Gen), Pred) :-
  874    '$get_predicate_attribute'(Pred, last_modified_generation, Gen).
  875'$predicate_property'(indexed(Indices), Pred) :-
  876    '$get_predicate_attribute'(Pred, indexed, Indices).
  877'$predicate_property'(noprofile, Pred) :-
  878    '$get_predicate_attribute'(Pred, noprofile, 1).
  879'$predicate_property'(ssu, Pred) :-
  880    '$get_predicate_attribute'(Pred, ssu, 1).
  881'$predicate_property'(iso, Pred) :-
  882    '$get_predicate_attribute'(Pred, iso, 1).
  883'$predicate_property'(det, Pred) :-
  884    '$get_predicate_attribute'(Pred, det, 1).
  885'$predicate_property'(sig_atomic, Pred) :-
  886    '$get_predicate_attribute'(Pred, sig_atomic, 1).
  887'$predicate_property'(quasi_quotation_syntax, Pred) :-
  888    '$get_predicate_attribute'(Pred, quasi_quotation_syntax, 1).
  889'$predicate_property'(defined, Pred) :-
  890    '$get_predicate_attribute'(Pred, defined, 1).
  891'$predicate_property'(tabled, Pred) :-
  892    '$get_predicate_attribute'(Pred, tabled, 1).
  893'$predicate_property'(tabled(Flag), Pred) :-
  894    '$get_predicate_attribute'(Pred, tabled, 1),
  895    table_flag(Flag, Pred).
  896'$predicate_property'(incremental, Pred) :-
  897    '$get_predicate_attribute'(Pred, incremental, 1).
  898'$predicate_property'(monotonic, Pred) :-
  899    '$get_predicate_attribute'(Pred, monotonic, 1).
  900'$predicate_property'(opaque, Pred) :-
  901    '$get_predicate_attribute'(Pred, opaque, 1).
  902'$predicate_property'(lazy, Pred) :-
  903    '$get_predicate_attribute'(Pred, lazy, 1).
  904'$predicate_property'(abstract(N), Pred) :-
  905    '$get_predicate_attribute'(Pred, abstract, N).
  906'$predicate_property'(size(Bytes), Pred) :-
  907    '$get_predicate_attribute'(Pred, size, Bytes).
  908'$predicate_property'(erased_clauses(Count), Pred) :-
  909    '$get_predicate_attribute'(Pred, erased_clauses, Count).
  910'$predicate_property'(erased_clause_space(Bytes), Pred) :-
  911    '$get_predicate_attribute'(Pred, erased_clause_space, Bytes).
  912'$predicate_property'(primary_index(Arg), Pred) :-
  913    '$get_predicate_attribute'(Pred, primary_index, Arg).
  914
  915system_undefined(user:prolog_trace_interception/4).
  916system_undefined(prolog:prolog_exception_hook/5).
  917system_undefined(system:'$c_call_prolog'/0).
  918system_undefined(system:window_title/2).
  919
  920table_flag(variant, Pred) :-
  921    '$tbl_implementation'(Pred, M:Head),
  922    M:'$tabled'(Head, variant).
  923table_flag(subsumptive, Pred) :-
  924    '$tbl_implementation'(Pred, M:Head),
  925    M:'$tabled'(Head, subsumptive).
  926table_flag(shared, Pred) :-
  927    '$get_predicate_attribute'(Pred, tshared, 1).
  928table_flag(incremental, Pred) :-
  929    '$get_predicate_attribute'(Pred, incremental, 1).
  930table_flag(monotonic, Pred) :-
  931    '$get_predicate_attribute'(Pred, monotonic, 1).
  932table_flag(subgoal_abstract(N), Pred) :-
  933    '$get_predicate_attribute'(Pred, subgoal_abstract, N).
  934table_flag(answer_abstract(N), Pred) :-
  935    '$get_predicate_attribute'(Pred, subgoal_abstract, N).
  936table_flag(subgoal_abstract(N), Pred) :-
  937    '$get_predicate_attribute'(Pred, max_answers, N).
 visible_predicate(:Head) is nondet
True when Head can be called without raising an existence error. This implies it is defined, can be inherited from a default module or can be autoloaded.
  946visible_predicate(Pred) :-
  947    Pred = M:Head,
  948    current_module(M),
  949    (   callable(Head)
  950    ->  (   '$get_predicate_attribute'(Pred, defined, 1)
  951        ->  true
  952        ;   \+ current_prolog_flag(M:unknown, fail),
  953            '$head_name_arity'(Head, Name, Arity),
  954            '$find_library'(M, Name, Arity, _LoadModule, _Library)
  955        )
  956    ;   setof(PI, visible_in_module(M, PI), PIs),
  957        '$member'(Name/Arity, PIs),
  958        functor(Head, Name, Arity)
  959    ).
  960
  961visible_in_module(M, Name/Arity) :-
  962    default_module(M, DefM),
  963    DefHead = DefM:Head,
  964    '$c_current_predicate'(_, DefHead),
  965    '$get_predicate_attribute'(DefHead, defined, 1),
  966    \+ hidden_system_predicate(Head),
  967    functor(Head, Name, Arity).
  968visible_in_module(_, Name/Arity) :-
  969    '$in_library'(Name, Arity, _).
  970
  971hidden_system_predicate(Head) :-
  972    functor(Head, Name, _),
  973    atom(Name),                     % Avoid [].
  974    sub_atom(Name, 0, _, _, $),
  975    \+ current_prolog_flag(access_level, system).
  976
  977
  978                /********************************
  979                *      SOURCE LOCATION          *
  980                *********************************/
 $predicate_source_location(:Head, -Location) is semidet
Location is File:Line, the place where the predicate Head is defined. Head is resolved to its primary definition, i.e., imported predicates are traced back to the module that defines them. For predicates defined in C, the location of the C function that implements it is used. See '$foreign_predicate_source'/2.

This predicate provides the source location for both the message system (see predicate_reference//2 in boot/messages.pl) and library(edit).

  994:- meta_predicate
  995    '$predicate_source_location'(:, -).  996
  997'$predicate_source_location'(Head, File:Line) :-
  998    '$primary_predicate'(Head, Primary),
  999    (   predicate_property(Primary, file(File)),
 1000        predicate_property(Primary, line_count(Line))
 1001    ->  true
 1002    ;   predicate_property(Primary, foreign),
 1003        '$foreign_source_location'(Primary, File, Line)
 1004    ).
 $primary_predicate(:Head, -Primary) is det
Primary is the module qualified Head in the module that defines the predicate rather than one that imports it.
 1011'$primary_predicate'(Pred, Primary) :-
 1012    (   predicate_property(Pred, imported_from(Module))
 1013    ->  strip_module(Pred, _, Head),
 1014        Primary = Module:Head
 1015    ;   Primary = Pred
 1016    ).
 $foreign_source_location(:Head, -File, -Line) is semidet
Source location of a foreign (C defined) predicate. As this is resolved by running addr2line or atos in a child process we can only afford to do this once per predicate.
 1024:- dynamic
 1025    '$foreign_source_cache'/2.              % PI, File:Line or `none`
 1026
 1027'$foreign_source_location'(Head, File, Line) :-
 1028    '$pi_head'(PI, Head),
 1029    (   '$foreign_source_cache'(PI, Cached)
 1030    ->  true
 1031    ;   (   '$foreign_predicate_source'(Head, Source),
 1032            '$addr2line_location'(Source, File0, Line0)
 1033        ->  Cached = File0:Line0
 1034        ;   Cached = none
 1035        ),
 1036        assertz('$foreign_source_cache'(PI, Cached))
 1037    ),
 1038    Cached = File:Line.
 $addr2line_location(+Description, -File, -Line) is semidet
Parse the Function() at File:Line description as produced by '$foreign_predicate_source'/2 and '$addr2line'/2. Fails if the description does not carry a source location, which happens if the symbol lives in the main executable or the file has no debug information.
 1048'$addr2line_location'(Description, File, Line) :-
 1049    sub_string(Description, Before, _, After, " at "),
 1050    !,
 1051    Start is Before+4,
 1052    sub_string(Description, Start, After, 0, Rest),
 1053    '$split_file_line'(Rest, File, Line).
 1054
 1055'$split_file_line'(Rest, File, Line) :-
 1056    sub_string(Rest, BC, _, AC, ":"),
 1057    Start is BC+1,
 1058    sub_string(Rest, Start, AC, 0, Tail),
 1059    (   sub_string(Tail, BS, _, _, " ")      % e.g. " (discriminator 1)"
 1060    ->  sub_string(Tail, 0, BS, _, LineText)
 1061    ;   LineText = Tail
 1062    ),
 1063    number_string(Line, LineText),
 1064    integer(Line),
 1065    Line >= 1,                               % `addr2line` says 0 if unknown
 1066    !,
 1067    sub_string(Rest, 0, BC, _, FileText),
 1068    FileText \== "??",                       % and `??` for the file
 1069    atom_string(File, FileText).
 clause_property(+ClauseRef, ?Property) is nondet
Provide information on individual clauses. Defined properties are:
line_count(-Line)
Line from which the clause is loaded.
file(-File)
File from which the clause is loaded.
source(-File)
File that `owns' the clause: reloading this file wipes the clause.
fact
Clause has body true.
erased
Clause was erased.
predicate(:PI)
Predicate indicator of the predicate this clause belongs to. Can be used to find the predicate of erased clauses.
module(-M)
Module context in which the clause was compiled.
 1094clause_property(Clause, Property) :-
 1095    '$clause_property'(Property, Clause).
 1096
 1097'$clause_property'(line_count(LineNumber), Clause) :-
 1098    '$get_clause_attribute'(Clause, line_count, LineNumber).
 1099'$clause_property'(file(File), Clause) :-
 1100    '$get_clause_attribute'(Clause, file, File).
 1101'$clause_property'(source(File), Clause) :-
 1102    '$get_clause_attribute'(Clause, owner, File).
 1103'$clause_property'(size(Bytes), Clause) :-
 1104    '$get_clause_attribute'(Clause, size, Bytes).
 1105'$clause_property'(fact, Clause) :-
 1106    '$get_clause_attribute'(Clause, fact, true).
 1107'$clause_property'(erased, Clause) :-
 1108    '$get_clause_attribute'(Clause, erased, true).
 1109'$clause_property'(predicate(PI), Clause) :-
 1110    '$get_clause_attribute'(Clause, predicate_indicator, PI).
 1111'$clause_property'(module(M), Clause) :-
 1112    '$get_clause_attribute'(Clause, module, M).
 dynamic(:Predicates, +Options) is det
Define a predicate as dynamic with optionally additional properties. Defined options are:
 1126dynamic(M:Predicates, Options) :-
 1127    '$must_be'(list, Predicates),
 1128    options_properties(Options, Props),
 1129    set_pprops(Predicates, M, [dynamic|Props]).
 1130
 1131set_pprops([], _, _).
 1132set_pprops([H|T], M, Props) :-
 1133    set_pprops1(Props, M:H),
 1134    strip_module(M:H, M2, P),
 1135    '$pi_head'(M2:P, Pred),
 1136    '$set_table_wrappers'(Pred),
 1137    set_pprops(T, M, Props).
 1138
 1139set_pprops1([], _).
 1140set_pprops1([H|T], P) :-
 1141    (   atom(H)
 1142    ->  '$set_predicate_attribute'(P, H, true)
 1143    ;   H =.. [Name,Value]
 1144    ->  '$set_predicate_attribute'(P, Name, Value)
 1145    ),
 1146    set_pprops1(T, P).
 1147
 1148options_properties(Options, Props) :-
 1149    G = opt_prop(_,_,_,_),
 1150    findall(G, G, Spec),
 1151    options_properties(Spec, Options, Props).
 1152
 1153options_properties([], _, []).
 1154options_properties([opt_prop(Name, Type, SetValue, Prop)|T],
 1155                   Options, [Prop|PT]) :-
 1156    Opt =.. [Name,V],
 1157    '$option'(Opt, Options),
 1158    '$must_be'(Type, V),
 1159    V = SetValue,
 1160    !,
 1161    options_properties(T, Options, PT).
 1162options_properties([_|T], Options, PT) :-
 1163    options_properties(T, Options, PT).
 1164
 1165opt_prop(incremental,   boolean,               Bool,  incremental(Bool)).
 1166opt_prop(abstract,      between(0,0),          0,     abstract).
 1167opt_prop(multifile,     boolean,               true,  multifile).
 1168opt_prop(discontiguous, boolean,               true,  discontiguous).
 1169opt_prop(volatile,      boolean,               true,  volatile).
 1170opt_prop(thread,        oneof(atom, [local,shared],[local,shared]),
 1171                                               local, thread_local).
 1172
 1173                /********************************
 1174                *            MODULES            *
 1175                *********************************/
 current_module(?Module) is nondet
True if Module is a currently defined module.
 1181current_module(Module) :-
 1182    '$current_module'(Module, _).
 module_property(?Module, ?Property) is nondet
True if Property is a property of Module. Defined properties are:
file(File)
Module is loaded from File.
line_count(Count)
The module declaration is on line Count of File.
exports(ListOfPredicateIndicators)
The module exports ListOfPredicateIndicators
exported_operators(ListOfOp3)
The module exports the operators ListOfOp3.
 1198module_property(Module, Property) :-
 1199    nonvar(Module), nonvar(Property),
 1200    !,
 1201    property_module(Property, Module).
 1202module_property(Module, Property) :-    % -, file(File)
 1203    nonvar(Property), Property = file(File),
 1204    !,
 1205    (   nonvar(File)
 1206    ->  '$current_module'(Modules, File),
 1207        (   atom(Modules)
 1208        ->  Module = Modules
 1209        ;   '$member'(Module, Modules)
 1210        )
 1211    ;   '$current_module'(Module, File),
 1212        File \== []
 1213    ).
 1214module_property(Module, Property) :-
 1215    current_module(Module),
 1216    property_module(Property, Module).
 1217
 1218property_module(Property, Module) :-
 1219    module_property(Property),
 1220    (   Property = exported_operators(List)
 1221    ->  '$exported_ops'(Module, List, [])
 1222    ;   '$module_property'(Module, Property)
 1223    ).
 1224
 1225module_property(class(_)).
 1226module_property(file(_)).
 1227module_property(line_count(_)).
 1228module_property(exports(_)).
 1229module_property(exported_operators(_)).
 1230module_property(size(_)).
 1231module_property(program_size(_)).
 1232module_property(program_space(_)).
 1233module_property(last_modified_generation(_)).
 module(+Module) is det
Set the module that is associated to the toplevel to Module.
 1239module(Module) :-
 1240    atom(Module),
 1241    current_module(Module),
 1242    !,
 1243    '$set_typein_module'(Module).
 1244module(Module) :-
 1245    '$set_typein_module'(Module),
 1246    print_message(warning, no_current_module(Module)).
 abolish_module(+Module) is det
Reset Module to a new empty module. If Module was loaded from a file, this file is unloaded. See '$abolish_module'/1.
 1253abolish_module(Module) :-
 1254    (   atom(Module),
 1255        '$current_module'(Module, File),
 1256        File \== []
 1257    ->  true
 1258    ;   File = []
 1259    ),
 1260    '$abolish_module'(Module),
 1261    retractall(system:'$load_context_module'(_, Module, _)),
 1262    (   File == []
 1263    ->  true
 1264    ;   unload_file(File)
 1265    ).
 working_directory(-Old, +New)
True when Old is the current working directory and the working directory has been updated to New.
 1272working_directory(Old, New) :-
 1273    '$cwd'(Old),
 1274    (   Old == New
 1275    ->  true
 1276    ;   '$chdir'(New)
 1277    ).
 1278
 1279
 1280                 /*******************************
 1281                 *            TRIES             *
 1282                 *******************************/
 current_trie(?Trie) is nondet
True if Trie is the handle of an existing trie.
 1288current_trie(Trie) :-
 1289    current_blob(Trie, trie),
 1290    is_trie(Trie).
 trie_property(?Trie, ?Property)
True when Property is a property of Trie. Defined properties are:
value_count(Count)
Number of terms in the trie.
node_count(Count)
Number of nodes in the trie.
size(Bytes)
Number of bytes needed to store the trie.
hashed(Count)
Number of hashed nodes.
compiled_size(Bytes)
Size of the compiled representation (if the trie is compiled)
lookup_count(Count)
Number of data lookups on the trie
gen_call_count(Count)
Number of trie_gen/2 calls on this trie

Incremental tabling statistics:

invalidated(Count)
Number of times the trie was inivalidated
reevaluated(Count)
Number of times the trie was re-evaluated

Shared tabling statistics:

deadlock(Count)
Number of times the table was involved in a deadlock
wait(Count)
Number of times a thread had to wait for this table
 1326trie_property(Trie, Property) :-
 1327    current_trie(Trie),
 1328    trie_property(Property),
 1329    '$trie_property'(Trie, Property).
 1330
 1331trie_property(node_count(_)).
 1332trie_property(value_count(_)).
 1333trie_property(size(_)).
 1334trie_property(hashed(_)).
 1335trie_property(compiled_size(_)).
 1336                                                % below only when -DO_TRIE_STATS
 1337trie_property(lookup_count(_)).                 % is enabled in pl-trie.h
 1338trie_property(gen_call_count(_)).
 1339trie_property(invalidated(_)).                  % IDG stats
 1340trie_property(reevaluated(_)).
 1341trie_property(deadlock(_)).                     % Shared tabling stats
 1342trie_property(wait(_)).
 1343trie_property(idg_affected_count(_)).
 1344trie_property(idg_dependent_count(_)).
 1345trie_property(idg_size(_)).
 1346
 1347
 1348                /********************************
 1349                *      SYSTEM INTERACTION       *
 1350                *********************************/
 1351
 1352shell(Command) :-
 1353    shell(Command, 0).
 1354
 1355
 1356                 /*******************************
 1357                 *            SIGNALS           *
 1358                 *******************************/
 1359
 1360:- meta_predicate
 1361    on_signal(+, :, :),
 1362    current_signal(?, ?, :).
 on_signal(+Signal, -OldHandler, :NewHandler) is det
 1366on_signal(Signal, Old, New) :-
 1367    atom(Signal),
 1368    !,
 1369    '$on_signal'(_Num, Signal, Old, New).
 1370on_signal(Signal, Old, New) :-
 1371    integer(Signal),
 1372    !,
 1373    '$on_signal'(Signal, _Name, Old, New).
 1374on_signal(Signal, _Old, _New) :-
 1375    '$type_error'(signal_name, Signal).
 current_signal(?Name, ?SignalNumber, :Handler) is nondet
 1379current_signal(Name, Id, Handler) :-
 1380    between(1, 32, Id),
 1381    '$on_signal'(Id, Name, Handler, Handler).
 1382
 1383:- multifile
 1384    prolog:called_by/2. 1385
 1386prolog:called_by(on_signal(_,_,New), [New+1]) :-
 1387    (   new == throw
 1388    ;   new == default
 1389    ), !, fail.
 1390
 1391
 1392                 /*******************************
 1393                 *             I/O              *
 1394                 *******************************/
 1395
 1396format(Fmt) :-
 1397    format(Fmt, []).
 1398
 1399                 /*******************************
 1400                 *            FILES             *
 1401                 *******************************/
 absolute_file_name(+Term, -AbsoluteFile)
 1405absolute_file_name(Name, Abs) :-
 1406    atomic(Name),
 1407    !,
 1408    '$absolute_file_name'(Name, Abs).
 1409absolute_file_name(Term, Abs) :-
 1410    '$chk_file'(Term, [''], [access(read)], true, File),
 1411    !,
 1412    '$absolute_file_name'(File, Abs).
 1413absolute_file_name(Term, Abs) :-
 1414    '$chk_file'(Term, [''], [], true, File),
 1415    !,
 1416    '$absolute_file_name'(File, Abs).
 tmp_file_stream(-File, -Stream, +Options) is det
tmp_file_stream(+Encoding, -File, -Stream) is det
Create a temporary file and open it atomically. The second mode is for compatibility reasons.
 1424tmp_file_stream(Enc, File, Stream) :-
 1425    atom(Enc), var(File), var(Stream),
 1426    !,
 1427    '$tmp_file_stream'('', Enc, File, Stream).
 1428tmp_file_stream(File, Stream, Options) :-
 1429    current_prolog_flag(encoding, DefEnc),
 1430    '$option'(encoding(Enc), Options, DefEnc),
 1431    '$option'(extension(Ext), Options, ''),
 1432    '$tmp_file_stream'(Ext, Enc, File, Stream),
 1433    set_stream(Stream, file_name(File)).
 1434
 1435
 1436                /********************************
 1437                *        MEMORY MANAGEMENT      *
 1438                *********************************/
 garbage_collect is det
Invoke the garbage collector. The argument of the underlying '$garbage_collect'/1 is the debugging level to use during garbage collection. This only works if the system is compiled with the -DODEBUG cpp flag. Only to simplify maintenance.
 1447garbage_collect :-
 1448    '$garbage_collect'(0).
 set_prolog_stack(+Name, +Option) is det
Set a parameter for one of the Prolog stacks.
 1454set_prolog_stack(Stack, Option) :-
 1455    Option =.. [Name,Value0],
 1456    Value is Value0,
 1457    '$set_prolog_stack'(Stack, Name, _Old, Value).
 prolog_stack_property(?Stack, ?Property) is nondet
Examine stack properties.
 1463prolog_stack_property(Stack, Property) :-
 1464    stack_property(P),
 1465    stack_name(Stack),
 1466    Property =.. [P,Value],
 1467    '$set_prolog_stack'(Stack, P, Value, Value).
 1468
 1469stack_name(local).
 1470stack_name(global).
 1471stack_name(trail).
 1472
 1473stack_property(limit).
 1474stack_property(spare).
 1475stack_property(min_free).
 1476stack_property(low).
 1477stack_property(factor).
 1478
 1479
 1480		 /*******************************
 1481		 *            CLAUSE		*
 1482		 *******************************/
 rule(:Head, -Rule) is nondet
 rule(:Head, -Rule, Ref) is nondet
Similar to clause/2,3. but deals with clauses that do not use :- as neck.
 1490rule(Head, Rule) :-
 1491    '$rule'(Head, Rule0),
 1492    conditional_rule(Rule0, Rule1),
 1493    Rule = Rule1.
 1494rule(Head, Rule, Ref) :-
 1495    '$rule'(Head, Rule0, Ref),
 1496    conditional_rule(Rule0, Rule1),
 1497    Rule = Rule1.
 1498
 1499conditional_rule(?=>(Head, (!, Body)), Rule) =>
 1500    Rule = (Head => Body).
 1501conditional_rule(?=>(Head, !), Rule) =>
 1502    Rule = (Head => true).
 1503conditional_rule(?=>(Head, Body0), Rule),
 1504    split_on_cut(Body0, Cond, Body) =>
 1505    Rule = (Head,Cond=>Body).
 1506conditional_rule(Head, Rule) =>
 1507    Rule = Head.
 1508
 1509split_on_cut((Cond0,!,Body0), Cond, Body) =>
 1510    Cond = Cond0,
 1511    Body = Body0.
 1512split_on_cut((!,Body0), Cond, Body) =>
 1513    Cond = true,
 1514    Body = Body0.
 1515split_on_cut((A,B), Cond, Body) =>
 1516    Cond = (A,Cond1),
 1517    split_on_cut(B, Cond1, Body).
 1518split_on_cut(_, _, _) =>
 1519    fail.
 1520
 1521
 1522                 /*******************************
 1523                 *             TERM             *
 1524                 *******************************/
 1525
 1526:- '$iso'((numbervars/3)).
 numbervars(+Term, +StartIndex, -EndIndex) is det
Number all unbound variables in Term using '$VAR'(N), where the first N is StartIndex and EndIndex is unified to the index that will be given to the next variable.
 1534numbervars(Term, From, To) :-
 1535    numbervars(Term, From, To, []).
 1536
 1537
 1538                 /*******************************
 1539                 *            STRING            *
 1540                 *******************************/
 term_string(?Term, ?String, +Options)
Parse/write a term from/to a string using Options. The predicate is module transparent, such that e.g., the portray_goal option is resolved in the context of the caller.
 1548:- module_transparent term_string/3. 1549
 1550term_string(Term, String, Options) :-
 1551    nonvar(String),
 1552    !,
 1553    read_term_from_atom(String, Term, Options).
 1554term_string(Term, String, Options) :-
 1555    (   '$option'(quoted(_), Options)
 1556    ->  Options1 = Options
 1557    ;   '$merge_options'(_{quoted:true}, Options, Options1)
 1558    ),
 1559    format(string(String), '~W', [Term, Options1]).
 1560
 1561
 1562		 /*******************************
 1563		 *            THREADS		*
 1564		 *******************************/
 1565
 1566:- meta_predicate
 1567    thread_create(0, -).
 thread_create(:Goal, -Id)
Shorthand for thread_create(Goal, Id, []).
 1573thread_create(Goal, Id) :-
 1574    thread_create(Goal, Id, []).
 thread_join(+Id)
Join a thread and raise an error of the thread did not succeed.
Errors
- thread_error(Status), where Status is the result of thread_join/2.
 1583thread_join(Id) :-
 1584    thread_join(Id, Status),
 1585    (   Status == true
 1586    ->  true
 1587    ;   throw(error(thread_error(Id, Status), _))
 1588    ).
 sig_block(:Pattern) is det
Block thread signals that unify with Pattern.
 sig_unblock(:Pattern) is det
Remove any signal block that is more specific than Pattern.
 1598sig_block(Pattern) :-
 1599    (   nb_current('$sig_blocked', List)
 1600    ->  true
 1601    ;   List = []
 1602    ),
 1603    nb_setval('$sig_blocked', [Pattern|List]).
 1604
 1605sig_unblock(Pattern) :-
 1606    (   nb_current('$sig_blocked', List)
 1607    ->  unblock(List, Pattern, NewList),
 1608        (   List == NewList
 1609        ->  true
 1610        ;   nb_setval('$sig_blocked', NewList),
 1611            '$sig_unblock'
 1612        )
 1613    ;   true
 1614    ).
 1615
 1616unblock([], _, []).
 1617unblock([H|T], P, List) :-
 1618    (   subsumes_term(P, H)
 1619    ->  unblock(T, P, List)
 1620    ;   List = [H|T1],
 1621        unblock(T, P, T1)
 1622    ).
 1623
 1624:- public signal_is_blocked/1.          % called by signal_is_blocked()
 1625
 1626signal_is_blocked(Head) :-
 1627    nb_current('$sig_blocked', List),
 1628    memberchk(Head, List).
 set_prolog_gc_thread(+Status)
Control the GC thread. Status is one of
false
Disable the separate GC thread, running atom and clause garbage collection in the triggering thread.
true
Enable the separate GC thread. All implicit atom and clause garbage collection is executed by the thread gc.
stop
Stop the gc thread if it is running. The thread is recreated on the next implicit atom or clause garbage collection. Used by fork/1 to avoid forking a multi-threaded application.
 1645set_prolog_gc_thread(Status) :-
 1646    var(Status),
 1647    !,
 1648    '$instantiation_error'(Status).
 1649set_prolog_gc_thread(_) :-
 1650    \+ current_prolog_flag(threads, true),
 1651    !.
 1652set_prolog_gc_thread(false) :-
 1653    !,
 1654    set_prolog_flag(gc_thread, false),
 1655    (   current_prolog_flag(threads, true)
 1656    ->  (   '$gc_stop'
 1657        ->  thread_join(gc)
 1658        ;   true
 1659        )
 1660    ;   true
 1661    ).
 1662set_prolog_gc_thread(true) :-
 1663    !,
 1664    set_prolog_flag(gc_thread, true).
 1665set_prolog_gc_thread(stop) :-
 1666    !,
 1667    (   current_prolog_flag(threads, true)
 1668    ->  (   '$gc_stop'
 1669        ->  thread_join(gc)
 1670        ;   true
 1671        )
 1672    ;   true
 1673    ).
 1674set_prolog_gc_thread(Status) :-
 1675    '$domain_error'(gc_thread, Status).
 transaction(:Goal)
 transaction(:Goal, +Options)
 transaction(:Goal, :Constraint, +Mutex)
 snapshot(:Goal)
Wrappers to guarantee clean Module:Goal terms.
 1684transaction(Goal) :-
 1685    '$transaction'(Goal, []).
 1686transaction(Goal, Options) :-
 1687    '$transaction'(Goal, Options).
 1688transaction(Goal, Constraint, Mutex) :-
 1689    '$transaction'(Goal, Constraint, Mutex).
 1690snapshot(Goal) :-
 1691    '$snapshot'(Goal).
 1692
 1693
 1694		 /*******************************
 1695		 *            UNDO		*
 1696		 *******************************/
 1697
 1698:- meta_predicate
 1699    undo(0).
 undo(:Goal)
Schedule Goal to be called when backtracking takes us back to before this call.
 1706undo(Goal) :-
 1707    '$undo'(Goal).
 1708
 1709:- public
 1710    '$run_undo'/1. 1711
 1712'$run_undo'([One]) :-
 1713    !,
 1714    (   call(One)
 1715    ->  true
 1716    ;   true
 1717    ).
 1718'$run_undo'(List) :-
 1719    run_undo(List, _, Error),
 1720    (   var(Error)
 1721    ->  true
 1722    ;   throw(Error)
 1723    ).
 1724
 1725run_undo([], E, E).
 1726run_undo([H|T], E0, E) :-
 1727    (   catch(H, E1, true)
 1728    ->  (   var(E1)
 1729        ->  true
 1730        ;   '$urgent_exception'(E0, E1, E2)
 1731        )
 1732    ;   true
 1733    ),
 1734    run_undo(T, E2, E).
 $wrap_predicate(:Head, +Name, -Closure, -Wrapped, :Body) is det
Would be nicer to have this from library(prolog_wrap), but we need it for tabling, so it must be a system predicate.
 1742:- meta_predicate
 1743    '$wrap_predicate'(:, +, -, -, 0). 1744
 1745'$wrap_predicate'(M:Head, WName, Closure, call(Wrapped), Body) :-
 1746    callable_name_arguments(Head, PName, Args),
 1747    callable_name_arity(Head, PName, Arity),
 1748    (   is_most_general_term(Head)
 1749    ->  true
 1750    ;   '$domain_error'(most_general_term, Head)
 1751    ),
 1752    atomic_list_concat(['$wrap$', PName], WrapName),
 1753    PI = M:WrapName/Arity,
 1754    dynamic(PI),
 1755    '$notransact'(PI),
 1756    volatile(PI),
 1757    module_transparent(PI),
 1758    WHead =.. [WrapName|Args],
 1759    wrapped_clause(M, WHead, Body, Clause),
 1760    '$c_wrap_predicate'(M:Head, WName, Closure, Wrapped, Clause).
 1761
 1762callable_name_arguments(Head, PName, Args) :-
 1763    atom(Head),
 1764    !,
 1765    PName = Head,
 1766    Args = [].
 1767callable_name_arguments(Head, PName, Args) :-
 1768    compound_name_arguments(Head, PName, Args).
 1769
 1770callable_name_arity(Head, PName, Arity) :-
 1771    atom(Head),
 1772    !,
 1773    PName = Head,
 1774    Arity = 0.
 1775callable_name_arity(Head, PName, Arity) :-
 1776    compound_name_arity(Head, PName, Arity).
 1777
 1778wrapped_clause(M, WHead, M:Body, M:(WHead :- Body)) :- !.
 1779wrapped_clause(M, WHead, MB:Body, M:(WHead :- MB:Body))