36
37:- module(lambda, [
38 (^)/3, (^)/4, (^)/5, (^)/6, (^)/7, (^)/8, (^)/9,
39 (\)/1, (\)/2, (\)/3, (\)/4, (\)/5, (\)/6, (\)/7,
40 (+\)/2, (+\)/3, (+\)/4, (+\)/5, (+\)/6, (+\)/7,
41 op(201,xfx,+\)]).
117:- meta_predicate no_hat_call(0). 118
119:- meta_predicate
120 ^(?,0,?),
121 ^(?,1,?,?),
122 ^(?,2,?,?,?),
123 ^(?,3,?,?,?,?),
124 ^(?,4,?,?,?,?,?),
125 ^(?,5,?,?,?,?,?,?),
126 ^(?,6,?,?,?,?,?,?,?). 127
128
129
130^(V1,Goal,V1) :-
131 no_hat_call(Goal).
132^(V1,Goal,V1,V2) :-
133 call(Goal,V2).
134^(V1,Goal,V1,V2,V3) :-
135 call(Goal,V2,V3).
136^(V1,Goal,V1,V2,V3,V4) :-
137 call(Goal,V2,V3,V4).
138^(V1,Goal,V1,V2,V3,V4,V5) :-
139 call(Goal,V2,V3,V4,V5).
140^(V1,Goal,V1,V2,V3,V4,V5,V6) :-
141 call(Goal,V2,V3,V4,V5,V6).
142^(V1,Goal,V1,V2,V3,V4,V5,V6,V7) :-
143 call(Goal,V2,V3,V4,V5,V6,V7).
144
145:- meta_predicate
146 \(0),
147 \(1,?),
148 \(2,?,?),
149 \(3,?,?,?),
150 \(4,?,?,?,?),
151 \(5,?,?,?,?,?),
152 \(6,?,?,?,?,?,?). 153
154\(FC) :-
155 copy_term_nat(FC,C),no_hat_call(C).
156\(FC,V1) :-
157 copy_term_nat(FC,C),call(C,V1).
158\(FC,V1,V2) :-
159 copy_term_nat(FC,C),call(C,V1,V2).
160\(FC,V1,V2,V3) :-
161 copy_term_nat(FC,C),call(C,V1,V2,V3).
162\(FC,V1,V2,V3,V4) :-
163 copy_term_nat(FC,C),call(C,V1,V2,V3,V4).
164\(FC,V1,V2,V3,V4,V5) :-
165 copy_term_nat(FC,C),call(C,V1,V2,V3,V4,V5).
166\(FC,V1,V2,V3,V4,V5,V6) :-
167 copy_term_nat(FC,C),call(C,V1,V2,V3,V4,V5,V6).
168
169:- meta_predicate
170 +\(?,0),
171 +\(?,1,?),
172 +\(?,2,?,?),
173 +\(?,3,?,?,?),
174 +\(?,4,?,?,?,?),
175 +\(?,5,?,?,?,?,?),
176 +\(?,6,?,?,?,?,?,?). 177
178+\(GV,FC) :-
179 copy_term_nat(GV+FC,GV+C),no_hat_call(C).
180+\(GV,FC,V1) :-
181 copy_term_nat(GV+FC,GV+C),call(C,V1).
182+\(GV,FC,V1,V2) :-
183 copy_term_nat(GV+FC,GV+C),call(C,V1,V2).
184+\(GV,FC,V1,V2,V3) :-
185 copy_term_nat(GV+FC,GV+C),call(C,V1,V2,V3).
186+\(GV,FC,V1,V2,V3,V4) :-
187 copy_term_nat(GV+FC,GV+C),call(C,V1,V2,V3,V4).
188+\(GV,FC,V1,V2,V3,V4,V5) :-
189 copy_term_nat(GV+FC,GV+C),call(C,V1,V2,V3,V4,V5).
190+\(GV,FC,V1,V2,V3,V4,V5,V6) :-
191 copy_term_nat(GV+FC,GV+C),call(C,V1,V2,V3,V4,V5,V6).
199no_hat_call(MGoal) :-
200 strip_module(MGoal, _, Goal),
201 ( nonvar(Goal),
202 Goal = (_^_)
203 -> throw(error(existence_error(lambda_parameters,Goal),_))
204 ; call(MGoal)
205 ).
206
Lambda expressions
This library provides lambda expressions to simplify higher order programming based on call/N.
Lambda expressions are represented by ordinary Prolog terms. There are two kinds of lambda expressions:
The second is a shorthand for t+\X1^X2^..^XN^Goal.
Xi are the parameters.
Goal is a goal or continuation. Syntax note: Operators within Goal require parentheses due to the low precedence of the ^ operator.
Free contains variables that are valid outside the scope of the lambda expression. They are thus free variables within.
All other variables of Goal are considered local variables. They must not appear outside the lambda expression. This restriction is currently not checked. Violations may lead to unexpected bindings.
In the following example the parentheses around X>3 are necessary.
In the following X is a variable that is shared by both instances of the lambda expression. The second query illustrates the cooperation of continuations and lambdas. The lambda expression is in this case a continuation expecting a further argument.
The following queries are all equivalent. To see this, use the fact
f(x,y)
.Further discussions http://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/ISO-Hiord