29
30:- module(terms_vars, [varset/2, intersect_vars/3, member_var/2, diff_vars/3,
31 varsbag/3, term_variables/2],
32 [assertions]). 33
34varset(Term, List) :- term_variables(Term, List).
35
36%-------------------------------------------------------------------------
37
38:- pred varset_in_args(T, LL) : nonvar(T) => list(LL, list(var)) # "Each
39 list of @var{LL} contains the variables of an argument of @var{T},
40 for each argument, and in left to right order.".
41
42varset_in_args(Term, Xss) :-
43 Term =.. [_|Args],
44 vars_in_args(Args, Xss).
45
46vars_in_args([], []).
47vars_in_args([Arg|Rest], [Arg_list|Rest_list]) :-
48 varset(Arg, Arg_list),
49 vars_in_args(Rest, Rest_list).
50
51intersect_vars([], _, []).
52intersect_vars([X|S1], S2, S) :-
53 ( member_var(S2, X) ->
54 S = [X|SList] ;
55 S = SList ),
56 intersect_vars(S1, S2, SList).
57
58diff_vars([], _L, []).
59diff_vars([H|L1], L2, L3) :-
60 member_var(L2, H),
61 !,
62 diff_vars(L1, L2, L3).
63diff_vars([H|L1], L2, [H|L3]) :-
64 diff_vars(L1, L2, L3).
65
67member_var([E|List], Ele) :-
68 E == Ele -> true ; member_var(List, Ele).
76varsbag(X, Vars, Tail) :-
77 var(X), !,
78 Vars = [X|Tail].
79varsbag([H|T], Vars, Tail) :- !,
80 varsbag(H, Vars, Tail0),
81 varsbag(T, Tail0, Tail).
82varsbag(Term, Vars, Tail) :-
83 functor(Term, _, A),
84 go_inside(A, Term, Vars, Tail).
85
86go_inside(0, _, Tail, Tail) :- !.
87go_inside(N, T, Bag, Tail) :-
88 Nth is N-1,
89 arg(N, T, ARG),
90 varsbag(ARG, Bag, Tail0),
91 go_inside(Nth, T, Tail0, Tail)