-
Notifications
You must be signed in to change notification settings - Fork 1
/
xpe.sex
333 lines (263 loc) · 10.6 KB
/
xpe.sex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; File: xpe.s ;;
;; Project: the specializer Unmix ;;
;; Author: S.A.Romanenko, the Institute for Applied ;;
;; Mathematics, the USSR Acedemy of Sciences, ;;
;; Moscow. ;;
;; Created: 5 May 1989 ;;
;; Revised: 8 April 1990 ;;
;; August 1990 ;;
;; ;;
;; Contents: The self-applicable part of ;;
;; the Partial Evaluator. ;;
;; ;;
;; Synopsis: ;;
;; (specialize-fundef ann-prog conf) ;;
;; ;;
;; ann-prog - an annotated Mixwell program ;;
;; conf - a computational configuration ;;
;; ;;
;; Description: ;;
;; The program constructs a residual function ;;
;; definition, "conf" being the "name" of the function ;;
;; to be generated. ;;
;; ;;
;; The configuration "conf" is a pair (fname . svv) ;;
;; where "fname" is the name of the dynamic function ;;
;; from "ann-prog", and "svv" is a list that contains ;;
;; the values of the function's static parameters to ;;
;; be used during the generation of the residual ;;
;; function definition. ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Data structures used by the partial evaluator:
;; =============================================
;;
;; Configuration:
;;
;; <Configuration> ::= ( <Fname> > . <Svv> )
;; <Svv> ::= ( <Lisp S-expression>* )
;;
;; A configuration is a possible runtime function call,
;; which causes the partial evaluator to produce a specialized
;; version of the function <Fname> with respect to the values
;; <Svv> of the static parameters.
;;
;; Naming conventions:
;; ==================
;;
;; ann-prog - the annotated Mixwell program
;; conf - a configuration
;; fundef - the annotated function definition
;; svn - a list of static variable names
;; dvn - a list of dynamic variable names
;; svv - a list of static variable values
;; dvv - a list of dynamic variable values
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; Generation of a Residual Function Definition ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; This function is to be called from other file
;; by "xcall".
;; If "conf" is nil, the function returns the name of
;; the goal function of the program. This trick is used
;; in order to separate this part of the partial evaluator from
;; the part that handles the set of pending configurations.
;; If "conf" is not nil, the function returns the list
;;
;; (dvn res-body)
;;
;; where "dnv" is the list of the parameters of the residual
;; function definition, and "res-body" is the residual body
;; of the function.
;;
(define ($specialize-fundef ann-prog conf)
(with (( (rf d-prog s-prog) ann-prog )
)
(select
(conf)
(() => (car rf))
((fname . svv) =>
(rcall ($check-function rf s-prog d-prog fname svv)))
)))
;;
;; This function searches through "rf", which is the list of
;; the residual function names, to find the name of the function
;; to be specialized. The looping through "rf" is done here and
;; not in a separate function to ensure that the function name
;; will be static when specializing this part of the partial
;; evaluator.
;;
(define ($check-function rf s-prog d-prog fname svv)
(select
(rf)
(() => '())
((rf-fname . rf-rest) =>
(if (eq? fname rf-fname)
($gen-res-fundef (assq rf-fname d-prog) s-prog d-prog svv)
($check-function rf-rest s-prog d-prog fname svv)))
))
;;
;; This function checks the list of static values
;; to ensure that its length is equal to the number
;; of the function's static parameters. This condition
;; is certain to be satisfied, unless the specializer
;; contains a bug, but this fact is difficult do discover
;; automatically...
;;
(define ($gen-res-fundef fundef s-prog d-prog svv)
(with* (( (fname svn dvn _ body) fundef )
( svv ($contract-svv svn svv) )
( new-body (rcall ($pe-exp body svn dvn svv dvn s-prog d-prog)) )
)
`(,dvn ,new-body)))
;;
;; The function "contract-svv" was intorduced to provide information
;; for the arity raiser. It is of little use, unless the partial
;; evaluator is specialized with respect to a program.
;;
(define ($contract-svv svn svv)
(select
(svn)
(() => '())
((_ . rest) =>
`(,(car svv) . ,($contract-svv rest (cdr svv))))
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; Partial (Symbolic) Evaluation of Expressions ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Returns a residual expression obtained by partially evaluating
;; the expression "exp" in an environment where the names of "svn"
;; and "dvn" are bound to the values of "svv" and "dvv".
;; The static values are ordinary Lisp S-expressions whereas
;; the dynamic values are some residual Mixwell expressions.
;;
(define ($pe-exp exp svn dvn svv dvv s-prog d-prog)
(select
(exp)
(_ & (symbol? exp) =>
($lookup-value exp dvn dvv))
(('static exp1) =>
`(quote ,($eval-exp exp1 svn svv s-prog)))
(('ifs exp1 exp2 exp3) =>
(if ($eval-exp exp1 svn svv s-prog)
($pe-exp exp2 svn dvn svv dvv s-prog d-prog)
($pe-exp exp3 svn dvn svv dvv s-prog d-prog)))
(('ifd exp1 exp2 exp3) =>
`(if ,($pe-exp exp1 svn dvn svv dvv s-prog d-prog)
,($pe-exp exp2 svn dvn svv dvv s-prog d-prog)
,($pe-exp exp3 svn dvn svv dvv s-prog d-prog)))
(('call fname s-exp* d-exp*) =>
($pe-call
(assq fname d-prog)
($eval-exp* s-exp* svn svv s-prog)
($pe-exp* d-exp* svn dvn svv dvv s-prog d-prog)
s-prog d-prog))
(('rcall fname s-exp* d-exp*) =>
`(call (,fname . ,($eval-exp* s-exp* svn svv s-prog))
. ,($pe-exp* d-exp* svn dvn svv dvv s-prog d-prog)))
(('xcall fname . exp*) =>
`(xcall ,fname
. ,($pe-exp* exp* svn dvn svv dvv s-prog d-prog)))
((fname . exp*) =>
`(,fname . ,($pe-exp* exp* svn dvn svv dvv s-prog d-prog)))
))
(define ($pe-exp* exp* svn dvn svv dvv s-prog d-prog)
(select
(exp*)
(() => '())
((exp . rest) =>
`(,($pe-exp exp svn dvn svv dvv s-prog d-prog)
. ,($pe-exp* rest svn dvn svv dvv s-prog d-prog)))
))
;;
;; Unfold a function call by inserting the (partially evaluated)
;; body of the called function.
;;
(define ($pe-call fundef svv dvv s-prog d-prog)
(with (( (_ svn dvn _ body) fundef )
)
(rcall ($pe-exp body svn dvn svv dvv s-prog d-prog))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; Evaluation of Static Expressions ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; This part of the partial evaluator is an ordinary
;; evaluator, which deals with static values only.
;;
(define ($eval-exp exp svn svv prog)
(select
(exp)
(_ & (symbol? exp) =>
($lookup-value exp svn svv))
(('quote s-exp) => s-exp)
(('if exp1 exp2 exp3) =>
(if ($eval-exp exp1 svn svv prog)
($eval-exp exp2 svn svv prog)
($eval-exp exp3 svn svv prog)))
(('call fname . exp*) =>
($eval-call
prog
(assq fname prog)
($eval-exp* exp* svn svv prog)))
(('xcall fname . exp*) =>
(xapply fname ($eval-exp* exp* svn svv prog)))
(('error . exp*) =>
(error "Error function encountered during partial evaluation"
`(error . ,($eval-exp* exp* svn svv prog))))
(('car exp1) =>
(car ($eval-exp exp1 svn svv prog)))
(('cdr exp1) =>
(cdr ($eval-exp exp1 svn svv prog)))
(('cons exp1 exp2) =>
(cons ($eval-exp exp1 svn svv prog)
($eval-exp exp2 svn svv prog)))
(('null? exp1) =>
(null? ($eval-exp exp1 svn svv prog)))
(('pair? exp1) =>
(pair? ($eval-exp exp1 svn svv prog)))
(('equal? exp1 exp2) =>
(equal? ($eval-exp exp1 svn svv prog)
($eval-exp exp2 svn svv prog)))
(('eq? exp1 exp2) =>
(eq? ($eval-exp exp1 svn svv prog)
($eval-exp exp2 svn svv prog)))
(('eqv? exp1 exp2) =>
(eqv? ($eval-exp exp1 svn svv prog)
($eval-exp exp2 svn svv prog)))
(('not exp1) =>
(not ($eval-exp exp1 svn svv prog)))
((fname . exp*) =>
(xapply fname ($eval-exp* exp* svn svv prog)))
))
(define ($eval-exp* exp* svn svv prog)
(select
(exp*)
(() => '())
((exp . rest) =>
`(,($eval-exp exp svn svv prog)
. ,($eval-exp* rest svn svv prog)))
))
(define ($eval-call prog fundef svv)
(with (( (_ svn _ body) fundef )
)
(rcall ($eval-exp body svn svv prog))))
;;
;; Search for "vname" in "vn" and return the corresponding
;; value from "vv".
;;
(define ($lookup-value vname vn vv)
(with (( (vnhd . vntl) vn )
( (vvhd . vvtl) vv )
)
(if (eq? vnhd vname)
vvhd
($lookup-value vname vntl vvtl))))