How to write in a standard conforming manner avs_term_rearranged(AVs, T, AVsR)
with given AVs
and T
such that AVsR
is a p
Use term_variables/2
on T
to obtain a list Xs
with variables in the desired order. Then build a list with the elements of AVs
, but in that order.
avs_term_rearranged(AVs, T, AVRs) :-
term_variables(T, Xs),
pick_in_order(AVs, Xs, AVRs).
pick_in_order([], [], []).
pick_in_order(AVs, [X|Xs], AVRs) :-
( pick(AVs, X, AV, AVs1) ->
AVRs = [AV|AVRs1],
pick_in_order(AVs1, Xs, AVRs1)
;
pick_in_order(AVs, Xs, AVRs)
).
pick([AV|AVs], X, AX, DAVs) :-
(_=V) = AV,
( V==X ->
AX = AV,
DAVs = AVs
;
DAVs = [AV|DAVs1],
pick(AVs, X, AX, DAVs1)
).
Notes:
pick/4
is linearterm_variables/2
is not strictly necessary, you could traverse T
directly