34
35:- module(foldargs, [foldargs//2,
36 foldargs//3,
37 foldargs//4,
38 foldargs//5
39 ]). 40
41:- meta_predicate
42 foldargs(4, +, +, -),
43 foldargs(5, +, +, +, -),
44 foldargs(6, +, +, +, +, -),
45 foldargs(7, +, +, +, +, +, -). 46
47foldargs(Goal, Term) -->
48 {compound(Term)},
49 !,
50 foldargs_(1, Goal, Term).
51foldargs(_, _) --> [].
52
53foldargs(Goal, T1, T2) -->
54 {compound(T1)},
55 !,
56 foldargs_(1, Goal, T1, T2).
57foldargs(_, _, _) --> [].
58
59foldargs(Goal, T1, T2, T3) -->
60 {compound(T1)},
61 !,
62 foldargs_(1, Goal, T1, T2, T3).
63foldargs(_, _, _, _) --> [].
64
65foldargs(Goal, T1, T2, T3, T4) -->
66 {compound(T1)},
67 !,
68 foldargs_(1, Goal, T1, T2, T3, T4).
69foldargs(_, _, _, _, _) --> [].
70
71foldargs_(N, Goal, T1) -->
72 {arg(N, T1, A1)},
73 !,
74 call(Goal, N, A1),
75 {succ(N, N1)},
76 foldargs_(N1, Goal, T1).
77foldargs_(_, _, _) --> [].
78
79foldargs_(N, Goal, T1, T2) -->
80 { arg(N, T1, A1),
81 arg(N, T2, A2)
82 },
83 !,
84 call(Goal, N, A1, A2),
85 {succ(N, N1)},
86 foldargs_(N1, Goal, T1, T2).
87foldargs_(_, _, _, _) --> [].
88
89foldargs_(N, Goal, T1, T2, T3) -->
90 { arg(N, T1, A1),
91 arg(N, T2, A2),
92 arg(N, T3, A3)
93 },
94 !,
95 call(Goal, N, A1, A2, A3),
96 {succ(N, N1)},
97 foldargs_(N1, Goal, T1, T2, T3).
98foldargs_(_, _, _, _, _) --> [].
99
100foldargs_(N, Goal, T1, T2, T3, T4) -->
101 { arg(N, T1, A1),
102 arg(N, T2, A2),
103 arg(N, T3, A3),
104 arg(N, T3, A4)
105 },
106 !,
107 call(Goal, N, A1, A2, A3, A4),
108 {succ(N, N1)},
109 foldargs_(N1, Goal, T1, T2, T3, T4).
110foldargs_(_, _, _, _, _, _) --> []