YAP 7.1.0
arg.yap
Go to the documentation of this file.
1/**
2 * @file library/arg.yap
3 * @author VITOR SANTOS COSTA <vsc@VITORs-MBP.lan>
4 * @date Tue Nov 17 01:08:55 2015
5 *
6 * @brief An extension of arg/3 that supports backtracking through a term.
7*/
8
9:- module(arg,
10 [
12 arg0/3,
14 args/3,
15 args0/3,
16 % project/3
18 ]).
19
20/**
21
22 @defgroup Args Extension of arg/3
23 *
24 * @brief An extension of arg/3 that supports backtracking through a term.
25 *
26 * @ingroup YAPLibrary
27 *
28 * @{
29 *
30 *This library extends arg/3 by supporting backtracking through
31 *arguments and access to sub-arguments,
32 *
33 * - arg0/3
34 * - args/3
35 * - args0/3
36 * - genarg/3
37 * - genarg0/3
38 * - path_arg/3
39 *
40 *
41 *It is based on the Quintus Prolog public domain library. Except for
42 *project, all predicates use the arg/3 argument pattern. This file has
43 *been included in the YAP library by Vitor Santos Costa, 2008.
44 *
45 * No error checking is actuallly performed within the package: this
46 *left to the C-code that implements arg/3 and genarg/3.
47 */
48
49
50/**
51 * @pred arg0( +_Index_, +_Term_ , -_Arg_ )
52 *
53 * Similar to arg/3, but `arg0(0,_T_,_F_)` unifies _F_ with _T_'s principal functor:
54
55```
56?- arg0(0, f(a,b), A).
57A = f.
58?- arg0(1, f(a,b), A).
59A = a.
60?- arg0(2, f(a,b), A).
61A = b.
62```
63
64*/
65arg0(0,T,A) :- arg0,
66 functor(T,A,_).
67arg0(I,T,A) :-
68 arg(I,T,A).
69
70/**
71 * @pred genarg0( +_Index_, +_Term_ , -_Arg_ )
72 *
73 * Similar to genarg/3, but `genarg0(0,_T_,_F_)` unifies _F_ with _T_'s principal functor:
74```
75?- genarg0(I,f(a,b),A).
76A = f,
77I = 0 ? ;
78A = a,
79I = 1 ? ;
80A = b,
81I = 2.
82```
83
84*/
85genarg0(I,T,A) :-
86 nonvar(I), nonvar,
87 arg0(I,T,A).
88genarg0(0,T,A) :-
89 functor(T,A,_).
90genarg0(I,T,A) :-
91 genarg(I,T,A).
92
93/**
94 * @pred args( +_Index_, +_ListOfTerms_ , -_ListOfArgs_ )
95 *
96 * Succeeds if _ListOfArgs_ unifies with the application of genarg/3 to every element of _ListOfTerms_.
97
98It corresponds to calling maplist/3 on genarg/3:
99```
100args( I, Ts, As) :-
101 maplist( genarg(I), Ts, As).
102```
103
104Notice that unification allows _ListOfArgs_ to be bound, eg:
105
106```
107?- args(1, [X1+Y1,X2-Y2,X3*Y3,X4/Y4], [1,1,1,1]).
108X1 = X2 = X3 = X4 = 1.
109```
110
111
112*/
113args(_,[],[]).
114args(I,[T|List],[A|ArgList]) :-
115 genarg(I, T, A),
116 args(I, List, ArgList).
117
118/**
119 * @pred args0( +_Index_, +_ListOfTerms_ , -_ListOfArgs_ )
120 *
121 * Succeeds if _ListOfArgs_ unifies with the application of genarg0/3 to every element of _ListOfTerms_.
122
123It corresponds to calling maplist/3 on genarg0/3:
124```
125args( I, Ts, As) :-
126 maplist( genarg0(I), Ts, As).
127```
128
129Notice that unification allows _ListOfArgs_ to be bound, eg:
130
131```
132?- args(1, [X1+Y1,X2-Y2,X3*Y3,X4/Y4], [1,1,1,1]).
133X1 = X2 = X3 = X4 = 1.
134```
135
136
137*/
138args0(_,[],[]).
139args0(I,[T|List],[A|ArgList]) :-
140 genarg(I, T, A),
141 args0(I, List, ArgList).
142
143/**
144 * @pred args0( +_ListOfTerms_ , +_Index_, -_ListOfArgs_ )
145 *
146 * Succeeds if _ListOfArgs_ unifies with the application of genarg0/3 to every element of _ListOfTerms_.
147
148It corresponds to calling args0/3 but with a different order.
149*/
150project(Terms, Index, Args) :-
151 args0(Index, Terms, Args).
152
153% no error checking here!
154/**
155 * @pred path_arg( +_Path_ , +_Term_, -_Arg_ )
156 *
157 * Succeeds if _Path_ is empty and _Arg unifies with _Term_, or if _Path_ is a list with _Head_ and _Tail_, genarg/3 succeeds on the current term, and path_arg/3 succeeds on its argument.
158 *
159 * Notice that it can be used to enumerate all possible paths in a term.
160*/
161path_arg([], Term, Term).
162path_arg([Index|Indices], Term, SubTerm) :-
163 genarg(Index, Term, Arg),
164 path_arg(Indices, Arg, SubTerm).
165
166%% @}
167
168
arg0( +_Index_, +_Term_ , -_Arg_ )
args0( +_Index_, +_ListOfTerms_ , -_ListOfArgs_ )
args( +_Index_, +_ListOfTerms_ , -_ListOfArgs_ )
genarg0( +_Index_, +_Term_ , -_Arg_ )
genarg( ?Index, +Term , -Arg )
path_arg( +_Path_ , +_Term_, -_Arg_ )
arg(+ N,+ T, A)
functor( T, F, N)
nonvar( T)