util.pl
1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*****************************************************************************/
/* */
/* Projecto de Compiladores ---------------------------------- VSPL */
/* Level 0 */
/* */
/* Utilities and administrivia. */
/* */
/* Salvador Pinto Abreu */
/* */
/*****************************************************************************/
% noop/1: cancel a series of operator definitions ----------------------------
noop([]) !.
noop([X|Xs]) <-> noop(Xs)
<= current_op(P, T, X), op(0, T, X) ; true.
:- noop([(type), (pred),
(div), (mod), (not),
(eq), (ge), (gt), (le), (lt), (ne)]).
% append/3 -------------------------------------------------------------------
append([], X, X) !.
append([A|X], Y, [A|Z]) <-> append(X, Y, Z).
% flatten/2, /3 --------------------------------------------------------------
flatten(L, Lf) <-> flatten(L, Lf, []).
flatten(V, X, X) ! <= var(V).
flatten([X|Xs], H, T) <-> flatten(X, H, T1), flatten(Xs, T1, T).
flatten([], X, X) !.
flatten(A, [A|X], X).
% wr/1: write out a term in a syntax acceptable to browser(1x) ---------------
wr(X) <-> writeq(X) <= var(X) ; atomic(X).
wr(X) <-> wrList(X, '[') <= functor(X, '.', 2).
wr(T) <-> T=..[F,A1|An], writeq(F), write('('), wr(A1), wr_(An), write(')').
wr_([]) !.
wr_([A1|An]) <-> write(','), wr(A1), wr_(An).
wrList(X, _) <-> write('|'), write(X), write(']') <= var(X).
wrList([], _) <-> write(']').
wrList(X, _) <-> write('|'), write(X), write(']') <= atomic(X).
wrList([H|T], S) <-> write(S), wr(H), wrList(T, ',').
wrList(X, _) <-> write('|'), wr(X), write(']').