1:- module(lambda, [
2		   (^)/3, (^)/4, (^)/5, (^)/6, (^)/7, (^)/8, (^)/9,
3		   (\)/1, (\)/2, (\)/3, (\)/4, (\)/5, (\)/6, (\)/7,
4		   (+\)/2, (+\)/3, (+\)/4, (+\)/5, (+\)/6, (+\)/7,
5		   op(201,xfx,+\)], eclipse_language).
6
7:- export initialization(
8    (
9	current_module_predicate(exported,P)@lambda,
10	\+current_predicate(P),
11	(import P from lambda),
12	printf(warning_output, "WARNING: library(lambda) redefines %Dw%n", [P]),
13	fail
14    ;
15	true
16    )).
17:- local op(1150,fx,meta_predicate).
18
19:- comment(categories, ["Programming Utilities"]).
20:- comment(author, "Ulrich Neumerkel, ulrich@complang.tuwien.ac.at (ECLiPSe port: Joachim Schimpf)").
21:- comment(copyright, "2009 Ulrich Neumerkel. All rights reserved.
22
23Redistribution and use in source and binary forms, with or without
24modification, are permitted provided that the following conditions are
25met:
26
271. Redistributions of source code must retain the above copyright
28   notice, this list of conditions and the following disclaimer.
29
302. Redistributions in binary form must reproduce the above copyright
31   notice, this list of conditions and the following disclaimer in the
32   documentation and/or other materials provided with the distribution.
33
34THIS SOFTWARE IS PROVIDED BY Ulrich Neumerkel ``AS IS'' AND ANY
35EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
37PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Ulrich Neumerkel OR
38CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
39EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
40PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
41PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
42LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
43NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
44SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45
46The views and conclusions contained in the software and documentation
47are those of the authors and should not be interpreted as representing
48official policies, either expressed or implied, of Ulrich Neumerkel.
49").
50
51:- comment(summary, "This library provides lambda expressions to simplify higher order
52programming based on call/N.").
53
54:- comment(desc, html("<PRE>
55/** <module> Lambda expressions
56
57This library provides lambda expressions to simplify higher order
58programming based on call/N.
59
60Lambda expressions are represented by ordinary Prolog terms.
61There are two kinds of lambda expressions:
62
63    Free+\\X1^X2^ ..^XN^Goal
64
65         \\X1^X2^ ..^XN^Goal
66
67The second is a shorthand for t+\\X1^X2^..^XN^Goal.
68
69Xi are the parameters.
70
71Goal is a goal or continuation. Syntax note: Operators within Goal
72require parentheses due to the low precedence of the ^ operator.
73
74Free contains variables that are valid outside the scope of the lambda
75expression. They are thus free variables within.
76
77All other variables of Goal are considered local variables. They must
78not appear outside the lambda expression. This restriction is
79currently not checked. Violations may lead to unexpected bindings.
80
81In the following example the parentheses around X>3 are necessary.
82
83==
84?- use_module(library(lambda)).
85?- use_module(library(apply)).
86
87?- maplist(\\X^(X>3),[4,5,9]).
88true.
89==
90
91In the following X is a variable that is shared by both instances of
92the lambda expression. The second query illustrates the cooperation of
93continuations and lambdas. The lambda expression is in this case a
94continuation expecting a further argument.
95
96==
97?- Xs = [A,B], maplist(X+\\Y^dif(X,Y), Xs).
98Xs = [A, B],
99dif(X, A),
100dif(X, B).
101
102?- Xs = [A,B], maplist(X+\\dif(X), Xs).
103Xs = [A, B],
104dif(X, A),
105dif(X, B).
106==
107
108The following queries are all equivalent. To see this, use
109the fact f(x,y).
110==
111?- call(f,A1,A2).
112?- call(\\X^f(X),A1,A2).
113?- call(\\X^Y^f(X,Y), A1,A2).
114?- call(\\X^(X+\\Y^f(X,Y)), A1,A2).
115?- call(call(f, A1),A2).
116?- call(f(A1),A2).
117?- f(A1,A2).
118A1 = x,
119A2 = y.
120==
121
122Further discussions
123http://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/ISO-Hiord
124
125@tbd Static expansion similar to apply_macros.
126@author Ulrich Neumerkel
127*/
128</PRE>")).
129
130:- meta_predicate no_hat_call(0).
131:- tool(no_hat_call/1, no_hat_call_/2).
132
133:- meta_predicate
134	^(?,0,?),
135	^(?,1,?,?),
136	^(?,2,?,?,?),
137	^(?,3,?,?,?,?),
138	^(?,4,?,?,?,?,?),
139	^(?,5,?,?,?,?,?,?),
140	^(?,6,?,?,?,?,?,?,?).
141:- tool((^)/3, '^_'/4).
142:- tool((^)/4, '^_'/5).
143:- tool((^)/5, '^_'/6).
144:- tool((^)/6, '^_'/7).
145:- tool((^)/7, '^_'/8).
146:- tool((^)/8, '^_'/9).
147:- tool((^)/9, '^_'/10).
148
149'^_'(V1,Goal,V1,M) :-
150   no_hat_call(Goal)@M.
151'^_'(V1,Goal,V1,V2,M) :-
152   call(Goal,V2)@M.
153'^_'(V1,Goal,V1,V2,V3,M) :-
154   call(Goal,V2,V3)@M.
155'^_'(V1,Goal,V1,V2,V3,V4,M) :-
156   call(Goal,V2,V3,V4)@M.
157'^_'(V1,Goal,V1,V2,V3,V4,V5,M) :-
158   call(Goal,V2,V3,V4,V5)@M.
159'^_'(V1,Goal,V1,V2,V3,V4,V5,V6,M) :-
160   call(Goal,V2,V3,V4,V5,V6)@M.
161'^_'(V1,Goal,V1,V2,V3,V4,V5,V6,V7,M) :-
162   call(Goal,V2,V3,V4,V5,V6,V7)@M.
163
164:- meta_predicate
165	\(0),
166	\(1,?),
167	\(2,?,?),
168	\(3,?,?,?),
169	\(4,?,?,?,?),
170	\(5,?,?,?,?,?),
171	\(6,?,?,?,?,?,?).
172:- tool((\)/1, '\\_'/2).
173:- tool((\)/2, '\\_'/3).
174:- tool((\)/3, '\\_'/4).
175:- tool((\)/4, '\\_'/5).
176:- tool((\)/5, '\\_'/6).
177:- tool((\)/6, '\\_'/7).
178:- tool((\)/7, '\\_'/8).
179
180'\\_'(FC,M) :-
181   copy_term(FC,C,_),no_hat_call(C)@M.
182'\\_'(FC,V1,M) :-
183   copy_term(FC,C,_),call(C,V1)@M.
184'\\_'(FC,V1,V2,M) :-
185   copy_term(FC,C,_),call(C,V1,V2)@M.
186'\\_'(FC,V1,V2,V3,M) :-
187   copy_term(FC,C,_),call(C,V1,V2,V3)@M.
188'\\_'(FC,V1,V2,V3,V4,M) :-
189   copy_term(FC,C,_),call(C,V1,V2,V3,V4)@M.
190'\\_'(FC,V1,V2,V3,V4,V5,M) :-
191   copy_term(FC,C,_),call(C,V1,V2,V3,V4,V5)@M.
192'\\_'(FC,V1,V2,V3,V4,V5,V6,M) :-
193   copy_term(FC,C,_),call(C,V1,V2,V3,V4,V5,V6)@M.
194
195:- meta_predicate
196	+\(?,0),
197	+\(?,1,?),
198	+\(?,2,?,?),
199	+\(?,3,?,?,?),
200	+\(?,4,?,?,?,?),
201	+\(?,5,?,?,?,?,?),
202	+\(?,6,?,?,?,?,?,?).
203:- tool((+\)/2, '+\\_'/3).
204:- tool((+\)/3, '+\\_'/4).
205:- tool((+\)/4, '+\\_'/5).
206:- tool((+\)/5, '+\\_'/6).
207:- tool((+\)/6, '+\\_'/7).
208:- tool((+\)/7, '+\\_'/8).
209:- tool((+\)/8, '+\\_'/9).
210
211'+\\_'(GV,FC,M) :-
212   copy_term(GV+FC,GV+C,_),no_hat_call(C)@M.
213'+\\_'(GV,FC,V1,M) :-
214   copy_term(GV+FC,GV+C,_),call(C,V1)@M.
215'+\\_'(GV,FC,V1,V2,M) :-
216   copy_term(GV+FC,GV+C,_),call(C,V1,V2)@M.
217'+\\_'(GV,FC,V1,V2,V3,M) :-
218   copy_term(GV+FC,GV+C,_),call(C,V1,V2,V3)@M.
219'+\\_'(GV,FC,V1,V2,V3,V4,M) :-
220   copy_term(GV+FC,GV+C,_),call(C,V1,V2,V3,V4)@M.
221'+\\_'(GV,FC,V1,V2,V3,V4,V5,M) :-
222   copy_term(GV+FC,GV+C,_),call(C,V1,V2,V3,V4,V5)@M.
223'+\\_'(GV,FC,V1,V2,V3,V4,V5,V6,M) :-
224   copy_term(GV+FC,GV+C,_),call(C,V1,V2,V3,V4,V5,V6)@M.
225
226
227%% no_hat_call(:Goal)
228%
229% Like call, but issues an error for a goal (^)/2.  Such goals are
230% likely the result of an insufficient number of arguments.
231
232no_hat_call_(Goal, M) :-
233   (  nonvar(Goal),
234      Goal = (_^_)
235   -> throw(error(existence_error(lambda_parameters,Goal),_))
236   ;  call(Goal)@M
237   ).
238
239% I would like to replace this by:
240% V1^Goal :- throw(error(existence_error(lambda_parameters,V1^Goal),_)).
241