1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2%%                                                                     %%
3%%                   The YapTab/YapOr/OPTYap systems                   %%
4%%                                                                     %%
5%% YapTab extends the Yap Prolog engine to support sequential tabling  %%
6%% YapOr extends the Yap Prolog engine to support or-parallelism       %%
7%% OPTYap extends the Yap Prolog engine to support or-parallel tabling %%
8%%                                                                     %%
9%%                                                                     %%
10%%      Yap Prolog was developed at University of Porto, Portugal      %%
11%%                                                                     %%
12%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13
14:- meta_predicate
15   table(:),
16   is_tabled(:),
17   tabling_mode(:,?),
18   abolish_table(:),
19   show_table(:),
20   table_statistics(:),
21   table_statistics(:,:).
22
23
24
25%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
26%%                               table/1                               %%
27%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
28
29table(Pred) :-
30   '$current_module'(Mod),
31   '$do_table'(Mod,Pred).
32
33'$do_table'(Mod,Pred) :-
34    var(Pred), !,
35   '$do_error'(instantiation_error,table(Mod:Pred)).
36'$do_table'(_,Mod:Pred) :- !,
37   '$do_table'(Mod,Pred).
38'$do_table'(_,[]) :- !.
39'$do_table'(Mod,[HPred|TPred]) :- !,
40   '$do_table'(Mod,HPred),
41   '$do_table'(Mod,TPred).
42'$do_table'(Mod,(Pred1,Pred2)) :- !,
43   '$do_table'(Mod,Pred1),
44   '$do_table'(Mod,Pred2).
45'$do_table'(Mod,PredName/PredArity) :-
46   atom(PredName),
47   integer(PredArity),
48   functor(PredFunctor,PredName,PredArity), !,
49   '$set_table'(Mod,PredFunctor).
50'$do_table'(Mod,Pred) :-
51   '$do_error'(type_error(callable,Mod:Pred),table(Mod:Pred)).
52
53'$set_table'(Mod,PredFunctor) :-
54   '$undefined'('$c_table'(_,_),prolog), !,
55   functor(PredFunctor, PredName, PredArity),
56   '$do_error'(resource_error(tabling,Mod:PredName/PredArity),table(Mod:PredName/PredArity)).
57'$set_table'(Mod,PredFunctor) :-
58   '$undefined'(PredFunctor,Mod), !,
59   '$c_table'(Mod,PredFunctor).
60'$set_table'(Mod,PredFunctor) :-
61   '$flags'(PredFunctor,Mod,Flags,Flags),
62   Flags /\ 0x1991F880 =:= 0,
63   '$c_table'(Mod,PredFunctor), !.
64'$set_table'(Mod,PredFunctor) :-
65   functor(PredFunctor,PredName,PredArity),
66   '$do_error'(permission_error(modify,table,Mod:PredName/PredArity),table(Mod:PredName/PredArity)).
67
68
69
70%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71%%                             is_tabled/1                             %%
72%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73
74is_tabled(Pred) :-
75   '$current_module'(Mod),
76   '$do_is_tabled'(Mod,Pred).
77
78'$do_is_tabled'(Mod,Pred) :-
79   var(Pred), !,
80   '$do_error'(instantiation_error,is_tabled(Mod:Pred)).
81'$do_is_tabled'(_,Mod:Pred) :- !,
82   '$do_is_tabled'(Mod,Pred).
83'$do_is_tabled'(_,[]) :- !.
84'$do_is_tabled'(Mod,[HPred|TPred]) :- !,
85   '$do_is_tabled'(Mod,HPred),
86   '$do_is_tabled'(Mod,TPred).
87'$do_is_tabled'(Mod,(Pred1,Pred2)) :- !,
88   '$do_is_tabled'(Mod,Pred1),
89   '$do_is_tabled'(Mod,Pred2).
90'$do_is_tabled'(Mod,PredName/PredArity) :-
91   atom(PredName),
92   integer(PredArity),
93   functor(PredFunctor,PredName,PredArity),
94   '$flags'(PredFunctor,Mod,Flags,Flags), !,
95   Flags /\ 0x000040 =\= 0.
96'$do_is_tabled'(Mod,Pred) :-
97   '$do_error'(type_error(callable,Mod:Pred),is_tabled(Mod:Pred)).
98
99
100
101%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102%%                            tabling_mode/2                           %%
103%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104
105tabling_mode(Pred,Options) :-
106   '$current_module'(Mod),
107   '$do_tabling_mode'(Mod,Pred,Options).
108
109'$do_tabling_mode'(Mod,Pred,Options) :-
110   var(Pred), !,
111   '$do_error'(instantiation_error,tabling_mode(Mod:Pred,Options)).
112'$do_tabling_mode'(_,Mod:Pred,Options) :- !,
113   '$do_tabling_mode'(Mod,Pred,Options).
114'$do_tabling_mode'(_,[],_) :- !.
115'$do_tabling_mode'(Mod,[HPred|TPred],Options) :- !,
116   '$do_tabling_mode'(Mod,HPred,Options),
117   '$do_tabling_mode'(Mod,TPred,Options).
118'$do_tabling_mode'(Mod,(Pred1,Pred2),Options) :- !,
119   '$do_tabling_mode'(Mod,Pred1,Options),
120   '$do_tabling_mode'(Mod,Pred2,Options).
121'$do_tabling_mode'(Mod,PredName/PredArity,Options) :-
122   atom(PredName),
123   integer(PredArity),
124   functor(PredFunctor,PredName,PredArity),
125   '$flags'(PredFunctor,Mod,Flags,Flags), !,
126   (
127       Flags /\ 0x000040 =\= 0, !, '$set_tabling_mode'(Mod,PredFunctor,Options)
128   ;
129       '$do_error'(domain_error(table,Mod:PredName/PredArity),tabling_mode(Mod:PredName/PredArity,Options))
130   ).
131'$do_tabling_mode'(Mod,Pred,Options) :-
132   '$do_error'(type_error(callable,Mod:Pred),tabling_mode(Mod:Pred,Options)).
133
134'$set_tabling_mode'(Mod,PredFunctor,Options) :-
135   var(Options), !,
136   '$c_tabling_mode'(Mod,PredFunctor,Options).
137'$set_tabling_mode'(_,_,[]) :- !.
138'$set_tabling_mode'(Mod,PredFunctor,[HOption|TOption]) :- !,
139   '$set_tabling_mode'(Mod,PredFunctor,HOption),
140   '$set_tabling_mode'(Mod,PredFunctor,TOption).
141'$set_tabling_mode'(Mod,PredFunctor,(Option1,Option2)) :- !,
142   '$set_tabling_mode'(Mod,PredFunctor,Option1),
143   '$set_tabling_mode'(Mod,PredFunctor,Option2).
144'$set_tabling_mode'(Mod,PredFunctor,Option) :-
145   '$transl_to_pred_flag_tabling_mode'(Flag,Option), !,
146   '$c_tabling_mode'(Mod,PredFunctor,Flag).
147'$set_tabling_mode'(Mod,PredFunctor,Options) :-
148   functor(PredFunctor,PredName,PredArity),
149   '$do_error'(domain_error(flag_value,tabling_mode+Options),tabling_mode(Mod:PredName/PredArity,Options)).
150
151%% should match with code in OPTYap/opt.preds.c
152'$transl_to_pred_flag_tabling_mode'(1,batched).
153'$transl_to_pred_flag_tabling_mode'(2,local).
154'$transl_to_pred_flag_tabling_mode'(3,exec_answers).
155'$transl_to_pred_flag_tabling_mode'(4,load_answers).
156'$transl_to_pred_flag_tabling_mode'(5,local_trie).
157'$transl_to_pred_flag_tabling_mode'(6,global_trie).
158
159
160
161%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
162%%                           abolish_table/1                           %%
163%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
164
165abolish_table(Pred) :-
166   '$current_module'(Mod),
167   '$do_abolish_table'(Mod,Pred).
168
169'$do_abolish_table'(Mod,Pred) :-
170   var(Pred), !,
171   '$do_error'(instantiation_error,abolish_table(Mod:Pred)).
172'$do_abolish_table'(_,Mod:Pred) :- !,
173   '$do_abolish_table'(Mod,Pred).
174'$do_abolish_table'(_,[]) :- !.
175'$do_abolish_table'(Mod,[HPred|TPred]) :- !,
176   '$do_abolish_table'(Mod,HPred),
177   '$do_abolish_table'(Mod,TPred).
178'$do_abolish_table'(Mod,(Pred1,Pred2)) :- !,
179   '$do_abolish_table'(Mod,Pred1),
180   '$do_abolish_table'(Mod,Pred2).
181'$do_abolish_table'(Mod,PredName/PredArity) :-
182   atom(PredName),
183   integer(PredArity),
184   functor(PredFunctor,PredName,PredArity),
185   '$flags'(PredFunctor,Mod,Flags,Flags), !,
186   (
187       Flags /\ 0x000040 =\= 0, !, '$c_abolish_table'(Mod,PredFunctor)
188   ;
189       '$do_error'(domain_error(table,Mod:PredName/PredArity),abolish_table(Mod:PredName/PredArity))
190   ).
191'$do_abolish_table'(Mod,Pred) :-
192   '$do_error'(type_error(callable,Mod:Pred),abolish_table(Mod:Pred)).
193
194
195
196%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
197%%                             show_table/1                            %%
198%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
199
200show_table(Pred) :-
201   '$current_module'(Mod),
202   '$do_show_table'(Mod,Pred).
203
204'$do_show_table'(Mod,Pred) :-
205   var(Pred), !,
206   '$do_error'(instantiation_error,show_table(Mod:Pred)).
207'$do_show_table'(_,Mod:Pred) :- !,
208   '$do_show_table'(Mod,Pred).
209'$do_show_table'(_,[]) :- !.
210'$do_show_table'(Mod,[HPred|TPred]) :- !,
211   '$do_show_table'(Mod,HPred),
212   '$do_show_table'(Mod,TPred).
213'$do_show_table'(Mod,(Pred1,Pred2)) :- !,
214   '$do_show_table'(Mod,Pred1),
215   '$do_show_table'(Mod,Pred2).
216'$do_show_table'(Mod,PredName/PredArity) :-
217   atom(PredName),
218   integer(PredArity),
219   functor(PredFunctor,PredName,PredArity),
220   '$flags'(PredFunctor,Mod,Flags,Flags), !,
221   (
222       Flags /\ 0x000040 =\= 0, !, '$c_show_table'(Mod,PredFunctor)
223   ;
224       '$do_error'(domain_error(table,Mod:PredName/PredArity),show_table(Mod:PredName/PredArity))
225   ).
226'$do_show_table'(Mod,Pred) :-
227   '$do_error'(type_error(callable,Mod:Pred),show_table(Mod:Pred)).
228
229
230
231%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
232%%                         table_statistics/1                          %%
233%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234
235table_statistics(Pred) :-
236   '$current_module'(Mod),
237   '$do_table_statistics'(Mod,Pred).
238
239'$do_table_statistics'(Mod,Pred) :-
240   var(Pred), !,
241   '$do_error'(instantiation_error,table_statistics(Mod:Pred)).
242'$do_table_statistics'(_,Mod:Pred) :- !,
243   '$do_table_statistics'(Mod,Pred).
244'$do_table_statistics'(_,[]) :- !.
245'$do_table_statistics'(Mod,[HPred|TPred]) :- !,
246   '$do_table_statistics'(Mod,HPred),
247   '$do_table_statistics'(Mod,TPred).
248'$do_table_statistics'(Mod,(Pred1,Pred2)) :- !,
249   '$do_table_statistics'(Mod,Pred1),
250   '$do_table_statistics'(Mod,Pred2).
251'$do_table_statistics'(Mod,PredName/PredArity) :-
252   atom(PredName),
253   integer(PredArity),
254   functor(PredFunctor,PredName,PredArity),
255   '$flags'(PredFunctor,Mod,Flags,Flags), !,
256   (
257       Flags /\ 0x000040 =\= 0, !, '$c_table_statistics'(Mod,PredFunctor)
258   ;
259       '$do_error'(domain_error(table,Mod:PredName/PredArity),table_statistics(Mod:PredName/PredArity))
260   ).
261'$do_table_statistics'(Mod,Pred) :-
262   '$do_error'(type_error(callable,Mod:Pred),table_statistics(Mod:Pred)).
263
264
265
266%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
267%%                        tabling_statistics/2                         %%
268%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
269
270%% should match with code in OPTYap/opt.preds.c
271tabling_statistics(total_memory,[BytesInUse,BytesAllocated]) :-
272   '$c_get_optyap_statistics'(0,BytesInUse,BytesAllocated).
273tabling_statistics(table_entries,[BytesInUse,StructsInUse]) :-
274   '$c_get_optyap_statistics'(1,BytesInUse,StructsInUse).
275tabling_statistics(subgoal_frames,[BytesInUse,StructsInUse]) :-
276   '$c_get_optyap_statistics'(2,BytesInUse,StructsInUse).
277tabling_statistics(dependency_frames,[BytesInUse,StructsInUse]) :-
278   '$c_get_optyap_statistics'(3,BytesInUse,StructsInUse).
279tabling_statistics(subgoal_trie_nodes,[BytesInUse,StructsInUse]) :-
280   '$c_get_optyap_statistics'(6,BytesInUse,StructsInUse).
281tabling_statistics(answer_trie_nodes,[BytesInUse,StructsInUse]) :-
282   '$c_get_optyap_statistics'(7,BytesInUse,StructsInUse).
283tabling_statistics(subgoal_trie_hashes,[BytesInUse,StructsInUse]) :-
284   '$c_get_optyap_statistics'(8,BytesInUse,StructsInUse).
285tabling_statistics(answer_trie_hashes,[BytesInUse,StructsInUse]) :-
286   '$c_get_optyap_statistics'(9,BytesInUse,StructsInUse).
287tabling_statistics(global_trie_nodes,[BytesInUse,StructsInUse]) :-
288   '$c_get_optyap_statistics'(10,BytesInUse,StructsInUse).
289tabling_statistics(global_trie_hashes,[BytesInUse,StructsInUse]) :-
290   '$c_get_optyap_statistics'(11,BytesInUse,StructsInUse).
291
292%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293