1:- module(meta,
2 [foldr/4, iterate/3,
3 for/3, foldnum/4,
4 fold/3, fold/4, fold/5,
5 do/3,
6 mapterm_rec/3,
7 maplist_opp/3,
8 repeat/2, monad/3, time/3,
9 until/4, while/4,
10 iff/2, cputime/1, cputime/0,
11 pred_eval/2, pred_eval/3
12 ]
13 ). 14
15
16:- use_module(pac(basic)). 17:- use_module(pac(op)).
24:- meta_predicate do(:, ?, ?). 25
27
28do(M:X, A, B):- once(do(X, A, B, M)).
30do(M:X, A, B, _) :-!, do(X, A, B, M).
31do((X,Y), A, B, M) :-!, do(X, A, C, M), do(Y, C, B, M).
32do((X;Y), A, B, M) :-!, ( do(X, A, B, M); do(Y, A, B, M)).
33do((X->Y), A, B, M) :-!, ( do(X, A, C, M) -> do(Y, C, B, M)).
34do({G}, A, A, M) :-!, call(M:G).
35do(X, A, B, M) :- call(M:X, A, B).
36
43user:local(X):- copy_term(X, Y), call(Y).
44user:local(X,A):- copy_term(X, Y), call(Y,A).
45user:local(X,A,B):- copy_term(X, Y), call(Y,A,B).
46user:local(X,A,B,C):- copy_term(X, Y), call(Y,A,B,C).
47user:local(X,A,B,C,D):- copy_term(X, Y), call(Y,A,B,C,D).
48user:local(X,A,B,C,D,E):- copy_term(X, Y), call(Y,A,B,C,D,E).
49
52iff(C0, C1) :- \+( (C0,\+C1) ; (C1, \+C0)).
53
59
60:- meta_predicate user:flip_arg(2, ?, ?). 61user:flip_arg(F, X, Y):- call(F, Y, X).
62
64user:flip_call(G) :- flip_call([2,1], G). 66user:flip_call(Is, G):- flip_call(Is, G, G0), call(G0).
67
68
69
71flip_call(Is, M:X, M:Y):-!, flip_call(Is, X, Y).
72flip_call(Is, X, Y):- length(Is, N),
73 functor(X, F, N),
74 functor(Y, F, N),
75 flip_call(Is, 1, X, Y).
76
78flip_call([], _, _, _).
79flip_call([I|Is], J, X, Y):-
80 arg(J, X, A),
81 arg(I, Y, A),
82 !,
83 J0 is J + 1,
84 flip_call(Is, J0, X, Y).
85
86:- meta_predicate foldr(3, ?, ?, ?). 87foldr(_, [], X, X):-!.
88foldr(F, [A|L], X, Y):- call(F, A, Y, Y0), foldr(F, L, X, Y0).
89
97fold_paths_of_term(G, T, X, Y):- fold_paths_of_term([[T]], [], X, Y, G).
99fold_paths_of_term([], _, X, X, _):-!.
100fold_paths_of_term([Ts|L], P, X, Y, G):-
101 ( Ts==[]
102 -> fold_paths_of_term(L, P, X, Y, G)
103 ; Ts=[T|Rs],
104 fold_paths_of_term(T, [Rs|L], P, X, Y, G)
105 ).
107fold_paths_of_term(T, Ls, P, X, Y, G):- atomic(T), !,
108 call(G, [T|P], X, Xtp),
109 fold_paths_of_term(Ls, P, Xtp, Y, G).
110fold_paths_of_term(T, Ls, P, X, Y, G):- T=..[F,A|As],
111 fold_paths_of_term(A, [As|Ls], [F|P], X, Y, G).
112
115fold_args(F, V, A, B):-
116 fold_args(1, F, V, A, B).
118fold_args(I, F, V, A, B):- arg(I, V, Vi), !,
119 call(F, Vi, A, Ai),
120 I1 is I+1,
121 fold_args(I1, F, V, Ai, B).
122fold_args(_I, _F, _V, _A, _B).
123
129
130
142
143
145callargs(F, A):- callargs(1, F, A).
147
148callargs(I, F, A):- arg(I, A, Ai), !,
149 call(F, Ai),
150 J is I+1,
151 callargs(J, F, A).
152callargs(_, _, _).
153
154
158
171
173
174
176scanargs(F, A, B):- ( var(A)
177 -> functor(B, C, N),
178 functor(A, C, N)
179 ; functor(A, C, N),
180 functor(B, C, N)
181 ),
182 scanargs(1, N, F, A, B).
184scanargs(I, N, _, _, _):- I > N, !.
185scanargs(I, N, F, A, B):- arg(I, A, Ai),
186 arg(I, B, Bi),
187 call(F, I, Ai, Bi),
188 J is I +1,
189 scanargs(J, N, F, A, B).
190
192scanargs(F, A):- functor(A, _, N),
193 scanargs_(1, N, F, A).
195scanargs_(I, N, _, _):- I > N, !.
196scanargs_(I, N, F, A):- arg(I, A, Ai),
197 call(F, I, Ai),
198 J is I + 1,
199 scanargs_(J, N, F, A).
200
201
203setargs(F, A, B):- functor(A, C, N),
204 ( var(B)
205 -> functor(B, C, N)
206 ; true
207 ),
208 setargs(1, N, F, A, B).
210setargs(I, N, _, _, _):- I > N, !.
211setargs(I, N, F, A, B):- arg(I, A, Ai),
212 call(F, I, Ai, Bi),
213 setarg(I, B, Bi),
214 J is I + 1,
215 setargs(J, N, F, A, B).
216
218setargs(F, A):- functor(A, _, N),
219 setargs_(1, N, F, A).
221setargs_(I, N, _, _):- I > N, !.
222setargs_(I, N, F, A):- arg(I, A, Ai),
223 call(F, I, Ai, Bi),
224 setarg(I, A, Bi),
225 J is I + 1,
226 setargs_(J, N, F, A).
227
232maprows(_, A, A):- atom(A), !.
233maprows(F, A, B):- arg(1, A, A1),
234 functor(A1, Fa, Na),
235 functor(B, Fa, Na),
236 maprows(Na, F, A, B).
238maprows(0, _, _, _):-!.
239maprows(I, F, A, B):- arg(I, B, Bi),
240 call(F, I, A, Bi),
241 J is I - 1,
242 maprows(J, F, A, B).
243
245until(F, S, X, Y):- call(S, X, X0),
246 ( call(F, X0) -> Y = X0
247 ; until(F, S, X0, Y)
248 ).
250iterate(_, stop(X), X):-!.
251iterate(S, X, Y):- call(S, X, X0),
252 iterate(S, X0, Y).
253
254while(F, S, X, Y):-
255 ( call(F, X) ->
256 call(S, X, X0),
257 while(F, S, X0, Y)
258 ; Y = X
259 ).
260
265
271
272:- meta_predicate for(?, 1). 273for(I..J, F):-!, for(I, J, F).
274for(I-J, F):-for(I, J, F).
275
277for(I, J, _):- I>J, !.
278for(I, J, F):- call(F, I), !,
279 I0 is I + 1,
280 for(I0, J, F).
281
286
287:- meta_predicate foldnum(3, ?, ?, ?). 288foldnum(F, I-J, U, V):-!, foldnum(I, J, U, V, F).
289foldnum(F, I..J, U, V):- foldnum(I, J, U, V, F).
291foldnum(I, J, U, U, _):- I>J, !.
292foldnum(I, J, U, V, F):- call(F, I, U, U0),
293 K is I + 1,
294 foldnum(K, J, U0, V, F).
295
297maplist_opp([F|Fs], X, [FX|Y]):- call(F, X, FX), !,
298 maplist_opp(Fs, X, Y).
299maplist_opp([], _, []).
300
302:- meta_predicate map(2, ?, ?). 303map(P) --> maplist(phrase(P)).
305:- meta_predicate phrase_list(2, ?, ?). 306phrase_list(P) --> maplist(phrase(P)).
307
311:- meta_predicate repeat(?, 0). 312repeat(1, G):-!, call(G).
313repeat(N, G):- simple_int_exp(N), !,
314 N0 is N,
315 (between(1, N0, _), call(G), fail; true).
316repeat(P, G):- (call(P), call(G), fail) ; true.
318repeat_cond(N, between(1, N0, _)):- simple_int_exp(N), !, N0 is N.
319repeat_cond(X, X).
321simple_int_exp(N):- integer(N).
322simple_int_exp(E):- ground(E),
323 functor(E, F, _),
324 memberchk(F, [+,-,^,mod, //]).
325
332
333
334:- meta_predicate time(0, ?). 335time(G, T):- time(G, T0, T1), T is T1-T0.
337time(G, T, T0):- statistics(cputime, T),
338 call(G),
339 statistics(cputime, T0).
340
348:- meta_predicate cputime(?, 0, ?). 350cputime(N, G, T):- writeln("Running pac runtime library cputime/3... "),
351 cputime(N, G, 0.00, T).
353cputime(0, _G, T, T).
354cputime(N, G, T, T0):- succ(N0, N),
355 cputime_for_step(G, S),
356 T1 is T + S,
357 cputime(N0, G, T1, T0).
359cputime_for_step(G, T):-
360 statistics(cputime, T0),
361 call(G),
362 statistics(cputime, T1),
363 T is T1-T0.
365cputime:- statistics(cputime, T), b_setval(cputime, T).
366cputime(T):- statistics(cputime, Stop), b_getval(cputime, Start), T is Stop - Start.
367
371
373:- meta_predicate monad(:, ?, ?). 374monad(G, X, Y):- once(ml:bind_context(G, (X, []), (Y, _))).
375
380
381pred_eval(X, Y):- pred_eval(call, X, Y).
382
383:- meta_predicate pred_eval(2, ?, ?). 384pred_eval(_, X, X):- ( var(X); number(X); string(X); is_list(X)), !.
385pred_eval(P, X, V):- atom(X), !, call(P, X, V).
386pred_eval(P, X, V):- functor(X, F, N),
387 functor(Y, F, N),
388 pred_eval_args(1, P, X, Y),
389 call(P, Y, V).
391pred_eval_args(I, P, X, Y):- arg(I, X, A), !,
392 arg(I, Y, B),
393 pred_eval(P, A, B),
394 J is I + 1,
395 pred_eval_args(J, P, X, Y).
396pred_eval_args(_, _, _, _).
397
398
400:- meta_predicate mapterm_rec(2, ?, ?). 401
403
404mapterm_rec(_, A, B):- var(A), !, B = A.
405mapterm_rec(F, A, B):- atomic(A), !, call(F, A, B).
406mapterm_rec(F, A, B):- functor(A, Fa, Na),
407 functor(B, Fa, Na),
408 mapterm_rec(F, 1, A, B).
410mapterm_rec(F, I, A, B):- arg(I, A, Ai), !,
411 arg(I, B, Bi),
412 mapterm_rec(F, Ai, Bi),
413 J is I + 1,
414 mapterm_rec(F, J, A, B).
415mapterm_rec(_, _, _, _):-!.
416
418:- meta_predicate maplist_rec(2, ?, ?). 419maplist_rec(_, [], []):-!.
420maplist_rec(F, [X|Xs], [Y|Ys]):-
421 ( X = [_|_]
422 -> maplist_rec(F, X, Y)
423 ; call(F, X, Y)
424 ),
425 maplist_rec(F, Xs, Ys).
426
429
430:- meta_predicate fold(?, :, :, ?, ?). 431fold(V, G, A, X, Y):- Acc = '$ACC'(X),
432 ( call(G),
433 arg(1, Acc, U),
434 call(A, V, U, W),
435 nb_setarg(1, Acc, W),
436 fail
437 ; arg(1, Acc, Y)
438 ).
439
442test_add_to_acc(X, Acc):- arg(1, Acc, V),
443 V0 is X + V,
444 nb_setarg(1, Acc, V0).
445
446:- meta_predicate fold(?, :, :, ?). 447fold(V, G, Fun, X):-
448 ( call(G),
449 call(Fun, V, X),
450 fail
451 ; true
452 ).
453
456:- meta_predicate fold(?, :, ?). 457fold(V, G, V^Act):-!,
458 ( call(G),
459 call(Act),
460 fail
461 ; true
462 ).
463fold(V, G, A):-
464 ( call(G),
465 call(A, V),
466 fail
467 ; true
468 ).
469
492
493det_foreach(Gen, Con):- term_variables(Gen, Vs),
494 sort(Vs, Vs0),
495 term_variables(Con, Ws),
496 sort(Ws, Ws0),
497 ord_subtr_var(Ws0, Vs0, Ws1),
498 once(foreach_by_copy(Gen, Con, Vs0, Ws1)).
500foreach(Gen, Con, Vs, Ws):- once(foreach_by_copy(Gen, Con, Vs, Ws)).
501
503foreach_by_copy(A, B, Vs, Ws):-
504 findall(Vs, A, Sol),
505 copy_of_goal(Sol, Vs, Ws, B, Cs),
506 maplist(call, Cs).
508copy_of_goal([], _, _, _, []).
509copy_of_goal([A|As], Vs, Ws, B, [G|Gs]):-
510 copy_term(Vs+Ws+B, A+Ws+G),
511 copy_of_goal(As, Vs, Ws, B, Gs).
513ord_subtr_var([], _, []):-!.
514ord_subtr_var(X, [], X):-!.
515ord_subtr_var([X|Xs], [Y|Ys], Zs):- X==Y, !,
516 ord_subtr_var(Xs, Ys, Zs).
517ord_subtr_var([X|Xs], [Y|Ys], [X|Zs]):- X@<Y, !,
518 ord_subtr_var(Xs, [Y|Ys], Zs).
519ord_subtr_var(Xs, [_|Ys], Zs):- ord_subtr_var(Xs, Ys, Zs).
520
521 524
537