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