```    1:- module(meta,
2	  [foldr/4, process/4,
3	   forin/4, for/3, numfold/5,
5	  iff/2, cputime/1, cputime/0]
6	 ).    7
8term_expansion('FLIP_CLAUSES'(N), Cs):- numlist(0, N, Ns),
9	maplist(create_flip_clause, Ns, Cs).
10
11
12% if-and-only-if condition test.
13
14% ?- meta:iff(true, member(X, [1,2])).
15iff(C0, C1) :- \+( (C0,\+C1) ; (C1, \+C0)).
16
17% ?- nopac(meta:flip([2,1], is, 1+1, R)).
18% ?- nopac(meta:flip([], true)).
19% ?- nopac(meta:flip([], writeln(hello))).
20% ?- nopac(meta:flip([1,3,2], append, [a,b], C, [c, d])).
21% ?- nopac(meta:flip([1, 2, 3], plus, 1, 2, X)).
22% ?- nopac(meta:flip([1, 3, 2], plus, 1, 2, X)).
23% ?- call(flip([1,3,2], append), [a,b], C, [c, d]).
24% ?- flip([1,3,2], append, [a,b], C, [c, d]).
25
26create_flip_clause(I, Clause) :- must_be(nonneg, I),
27	Clause = (H :- mpac:flip_list(Permu, As, Bs),  Call),
28	length(As, I),
29	length(Bs, I),
30	H =..[flip, Permu, F|As],
31	Call =..[call, F|Bs].
32
33% Example of expanded clauses.
34% flip(A, D, B, C) :-
35%     mpac:flip_list(A, [B, C], [E, F]),
36%     call(D, E, F).
37
38'FLIP_CLAUSES'(6).
39
40:- meta_predicate foldr(3, ?, ?, ?).   41foldr(_, [], X, X):-!.
42foldr(F, [A|L], X, Y):- call(F, A, Y, Y0), foldr(F, L, X, Y0).
43
44% ?- meta:foldR(cons, [a,b,c,d], [], R).
45% cons(X, Y, [X|Y]).
46% foldR(F, List, X, Y):-  foldl(swap_args(F), List, Y, X).
47% swap_args(F, X, Y, Z):- call(F, X, Z, Y).
48
49% ?- foldl(cons, [a,b,c,d], [], R).
50% ?- foldl(pred([X, Y, [X|Y]]), [1,2,3,4], [], R).
51% ?- foldr(pred([X, [X|Y], Y]), [1,2,3,4], R, []).
52% ?- foldr(flip([1,3,2], pred([X, [X|Y], Y])), [1,2,3,4], [], R).
53% ?- foldl(flip([1,3,2], pred([X, Y, [X|Y]])), [1,2,3,4], R, []).
54% ?- foldr(pred([X, Y, [X|Y]]), [1,2,3,4], [], R).
```
Starting with initial state S, repeat applying Delta to the successive states until the state satisfies the condition Final/1, with which S0 is unified.
```   61:- meta_predicate process(2, 1, +, -).   62process(_, Final, S, S):- call(Final, S).
63process(Delta, Final, S, S0):-
64		call(Delta, S, S1),
65		process(Delta, Final, S1, S0).
66%
67:- meta_predicate for(?, ?, 1).   68for(I, J, _):- I>J, !.
69for(I, J, F):- call(F, I), !,
70		I0 is I + 1,
71		for(I0, J, F).
72
73% ?-  F = plus, forin(F, 1-2, 0, R).
74% ?-  forin(plus, 1-2, 0, R).
75% ?-  forin(pred([X,Y,Z]:-plus(X, Y, Z)), 1-2, 0, R).
76% ?-  forin(pred([X,Y,Z]:-plus(X, Y, Z)), 1-2, 0, R).
77% ?-  mpac:let(X, pred([I,[I|U], U])), forin(X, 1-10, R, []).
78
79:- meta_predicate forin(3,  ?, ?, ?).   80forin(F, I-J, U, V):- numfold(F, I, J, U, V).
81
82%
83:- meta_predicate numfold(3, ?, ?, ?, ?).   84numfold(_, I, J, U, U):- I>J, !.
85numfold(F, I, J, U, V):- call(F, I, U, U0),
86	I0 is I + 1,
87	numfold(F, I0, J, U0, V).
88%
89:- meta_predicate map(2, ?, ?).   90map(P) --> maplist(phrase(P)).
91%
92:- meta_predicate phrase_list(2, ?, ?).   93phrase_list(P) --> maplist(phrase(P)).
94
95:- meta_predicate repeat(?, 0).   96% ?- repeat(3, writeln(hello)).
97repeat(N, _):- N =< 0, !.
98repeat(N, G):- G, !, N0 is N - 1, repeat(N0, G).
99
100%%% for statistics
101% ?- time(1000000, eval(desugar, -a-b, X), N).
102% N = 7.93
103:- meta_predicate time(?, 0, ?).  104time(N, G, T) :-  time0(repeat(N, G), T).
105
106time0(G, T):-  time0(G, T0, T1), T is T1-T0.
107
108time0(G, T, T0):-  statistics(cputime, T),
109	call(G),
110	statistics(cputime, T0).
111
112%
113% ?- cputime, A=(X=1), cputime(10000000, A, T), cputime(Total).
114%@ Running pac runtime library cputime/3...
115%@ A =  (1=1),
116%@ X = 1,
117%@ T = 2.6888779999965564,
118%@ Total = 9.716955000000002 .
119
120%@ Running pac runtime library cputime/3...
121%@ A =  (1=1),
122%@ X = 1,
123%@ T = 2.689835999999378,
124%@ Total = 9.69613
125
126% ?- cputime, cputime(10000000, X=1, T), cputime(Total).
127%@ running compiled cputime/3 ...
128%@ X = 1,
129%@ T = 1.8559170000025595,
130%@ Total = 8.999147 .
131
132% ?- cputime, cputime(10000000, (b_setval(x,1), b_getval(x, Y)), T), cputime(Total).
133%@ running compiled cputime/3 ...
134%@ Y = 1,
135%@ T = 3.054187999993445,
136%@ Total = 9.701370999999995 .
137%@ running compiled cputime/3 ...
138%@ Y = 1,
139%@ T = 3.037584000001438,
140%@ Total = 9.663783000000002 .
141
142% ?- cputime, cputime(10000000, (X=1, Y=X), T), cputime(Total).
143%@ running compiled cputime/3 ...
144%@ X = Y, Y = 1,
145%@ T = 3.0121809999994866,
146%@ Total = 10.495227 .
147%@ running compiled cputime/3 ...
148%@ X = Y, Y = 1,
149%@ T = 3.0290780000026842,
150%@ Total = 10.552797999999996 .
151
152%@ Running pac runtime library cputime/3...
153%@ 0.412335-0.325164
154%@ A =  (1=1),
155%@ X = 1,
156%@ T = 0.028493999999991637,
157%@ Total = 0.087171 .
158
159% ?- cputime, cputime(1000000, X=1, T), cputime(Total).
160%@ running compiled cputime/3 ...
161%@ X = 1,
162%@ T = 0.18144799999999,
163%@ Total = 0.8965290000000001 .
164
165%@ running compiled cputime/3 ...
166%@ X = 1,
167%@ T = 0.0023569999999991653,
168%@ Total = 0.009514999999999996 .
169
170:- meta_predicate cputime(?, 0, ?).  171%
172cputime(N, G, T):- writeln("Running pac runtime library cputime/3..."),
173				   cputime(N, G, 0.00, T).
174
175%
176cputime(0, _G,  T, T).
177cputime(N, G, T, T0):-  succ(N0, N),
178						cputime_for_once(G, S),
179						T1 is T + S,
180						cputime(N0, G, T1, T0).
181%
182cputime_for_once(G, T):-
183	statistics(cputime, T0),
184	call(G),
185	statistics(cputime, T1),
186	T is T1-T0.
187
188cputime:- statistics(cputime, T), b_setval(cputime, T).
189cputime(T):- statistics(cputime, Stop), b_getval(cputime, Start), T is Stop - Start.
190
191% cputime(T):- statistics(cputime, Stop), b_getval(cputime, Start), T is Stop - Start, writeln(Stop-Start).
192
193%
194
195% ?- cputime(10000000, true, T).
196% ?- meta:cputime(10000000, (A=a, B=A), T).
197% ?- meta:cputime(10000000, (b_setval(a, b), b_getval(a, B)), T).
198