YAP 7.1.0
utils.yap
Go to the documentation of this file.
1 /*************************************************************************
2 * *
3 * YAP Prolog *
4 * *
5 * Yap Prolog was developed at NCCUP - Universidade do Porto *
6 * *
7 * Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
8 * *
9 **************************************************************************
10 * *
11 * File: utils.yap *
12 * Last rev: 8/2/88 *
13 * mods: *
14 * comments: Some utility predicates available in yap *
15 * *
16 *************************************************************************/
17
18/**
19 * @file utils.yap
20 * @author VITOR SANTOS COSTA <vsc@VITORs-MBP-2.lan>
21 * @date Thu Oct 19 12:21:01 2017
22 *
23 * @brief Utilities
24 *
25 * @defgroup MixBag Diverse Utilities
26 * @ingroup Builtins
27 *
28 *
29 */
30
31
32 :-
33 .
34
35%%% current ....
36
37/** @pred simple( _T_)
38
39
40Checks whether _T_ is unbound, an atom, or a number.
41
42
43*/
44simple(V) :- var(V), var.
45simple(A) :- atom(A), atom.
46simple(N) :- number(N).
47
48/** @pred nth_instance(? _Key_,? _Index_,? _R_)
49
50
51Fetches the _Index_nth entry in the internal database under the key
52 _Key_. Entries are numbered from one. If the key _Key_ or the
53 _Index_ are bound, a reference is unified with _R_. Otherwise,
54the reference _R_ must be given, and YAP will find
55the matching key and index.
56
57
58*/
59nth_instance(Key,Index,Ref) :-
60 nonvar(Key), var(Index), var(Ref), var,
61 recorded(Key,_,Ref),
62 '$nth_instance'(_,Index,Ref).
63nth_instance(Key,Index,Ref) :-
64 '$nth_instance'(Key,Index,Ref).
65
66/** @pred nth_instance(? _Key_,? _Index_, _T_,? _R_)
67
68Fetches the _Index_nth entry in the internal database under the key
69 _Key_. Entries are numbered from one. If the key _Key_ or the
70 _Index_ are bound, a reference is unified with _R_. Otherwise,
71the reference _R_ must be given, and YAP will find
72the matching key and index.
73
74
75*/
76nth_instance(Key,Index,T,Ref) :-
77 nonvar(Key), var(Index), var(Ref), var,
78 recorded(Key,T,Ref),
79 '$nth_instance'(_,Index,Ref).
80nth_instance(Key,Index,T,Ref) :-
81 '$nth_instance'(Key,Index,Ref),
82 instance(Ref,T).
83
84/** @pred nb_current(? _Name_, ? _Value_)
85
86
87Enumerate all defined variables with their value. The order of
88enumeration is undefined.
89
90
91*/
92/** @pred nb_current(? _Name_,? _Value_)
93
94
95Enumerate all defined variables with their value. The order of
96enumeration is undefined.
97
98
99*/
100nb_current(GlobalVariable, Val) :-
101 '$nb_current'(GlobalVariable),
102 '__NB_getval__'(GlobalVariable, Val, _).
103
104'$getval_exception'(GlobalVariable, _Val, Caller) :-
105 '$getval_exception':exception(undefined_global_variable, GlobalVariable, Action),
106 exception,
107 (
108 Action == fail
109 ->
110 fail
111 ;
112 Action == retry
113 ->
114 true
115 ;
116 Action == error
117 ->
118 '$do_error'(existence_error(variable, GlobalVariable),Caller)
119 ;
120 '$do_error'(type_error(atom, Action),Caller)
121 ).
122
123
124/** @pred subsumes_term(? _Subsumer_, ? _Subsumed_)
125
126
127
128Succeed if _Submuser_ subsumes _Subsuned_ but does not bind any
129variable in _Subsumer_.
130
131
132*/
133subsumes_term(A,B) :-
134 \+ \+ subsumes_term:subsumes(A,B).
135
136term_string( T, S, Opts) :-
137 var( T ),
138 var,
139 var:open_mem_read_stream( S, Stream ),
140 read_term( Stream, T, Opts ),
141 close( Stream ).
142 term_string( T, S, _Opts) :-
143 format(string(S), '~q.~n', [T]).
144
145
146term_hash(T,H) :-
147 term_hash:term_hash(T, -1, 33554432, H).
148/*
149numbervars( T, I0, I) :-
150 term_variables(T, Vs),
151 bind_variables(Vs,I0,I).
152
153bind_variables([],I,I).
154bind_variables(['$VAR'(I0)|Vs],I0,I) :-
155 I1 is I0+1,
156 bind_variables(Vs,I1,I).
157*/
158
159
close(+ S)
nb_current(? Name, ? Value)
nth_instance(? Key,? Index,? R)
nth_instance(? Key,? Index, T,? R)
simple( T)
Definition: utils.yap:32
subsumes_term(? Subsumer, ? Subsumed)
instance(+ R,- T)
subsumes(? Term1, ? Term2)
term_hash(+ Term, ? Hash)
term_hash(+ Term, + Depth, + Range, ? Hash)
exception(+ Exception, + Context, - Action)
atom( T)
nonvar( T)
number( T)
var( T)