-
Notifications
You must be signed in to change notification settings - Fork 1
/
xarps.sex
320 lines (234 loc) · 7.6 KB
/
xarps.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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; File: xarps.sex ;;
;; Project: the specializer Unmix ;;
;; Author: S.A.Romanenko, the Institute for Applied ;;
;; Mathematics, the USSR Acedemy of Sciences, ;;
;; Moscow. ;;
;; Created: 5 May 1989 ;;
;; Revised: 7 December 1989 ;;
;; 10 April 1990 ;;
;; August 1990 ;;
;; ;;
;; Contents: The Parameter Splitter of the Arity Raiser ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; Parameter Splitting and Local Optimization ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Returns a program obtaind by optimizing the program "prog"
;; with the use of the description "types".
;;
(define (uarps:optimize prog types)
;;
;; Splits formal parameters and
;; returns an optimized function definition.
;;
(define (opt-fundef fundef)
(with* (( (fname parlist _ body) fundef )
( (_ . partypes) (assq fname types) )
( (new-parlist . exp*) (split-par* parlist partypes) )
( new-body (opt-exp body parlist exp*) )
)
`(,fname ,new-parlist = ,new-body)))
;;
;; Splits the arguments in function calls in the expression
;; "exp" and performs some local optimization.
;;
(define (opt-exp exp vn vv)
(select
(exp)
(_ & (symbol? exp) => (lookup-variable exp vn vv))
(('quote _) => exp)
(('car exp1) =>
(opt-car (opt-exp exp1 vn vv)))
(('cdr exp1) =>
(opt-cdr (opt-exp exp1 vn vv)))
(('pair? exp1) =>
(opt-pair? (opt-exp exp1 vn vv)))
(('cons exp1 exp2) =>
(opt-cons (opt-exp exp1 vn vv)
(opt-exp exp2 vn vv)))
(('equal? exp1 exp2) =>
(opt-equal? (opt-exp exp1 vn vv)
(opt-exp exp2 vn vv)))
(('if exp1 exp2 exp3) =>
(opt-if (opt-exp exp1 vn vv)
exp2 exp3 vn vv))
(('call fname . exp*) =>
(with* (( (_ . arg-types) (assq fname types) )
( exp* (opt-exp* exp* vn vv) )
)
`(call ,fname . ,(split-arg* exp* arg-types))))
(('xcall fname . exp*) =>
`(xcall ,fname . ,(opt-exp* exp* vn vv)))
((fname . exp*) =>
`(,fname . ,(opt-exp* exp* vn vv)))
))
(define (opt-exp* exp* vn vv)
(map (lambda (exp) (opt-exp exp vn vv)) exp*))
(define (opt-car exp)
(match
(exp)
(('quote (c1 . c2)) => `(quote ,c1))
(('cons e1 e2) => e1)
(_ => `(car ,exp))
))
(define (opt-cdr exp)
(match
(exp)
(('quote (c1 . c2)) => `(quote ,c2))
(('cons e1 e2) => e2)
(_ => `(cdr ,exp))
))
(define (opt-pair? exp)
(match
(exp)
(('quote c) => `(quote ,(pair? c)))
(('cons e1 e2) => ''#t)
(_ => `(pair? ,exp))
))
(define (opt-cons exp1 exp2)
(match
(exp1 exp2)
(('quote c1) ('quote c2) =>
`(quote (,c1 . ,c2)))
(_ _ => `(cons ,exp1 ,exp2))
))
(define (opt-equal? exp1 exp2)
(match
(exp1 exp2)
(('quote c1) ('quote c2) =>
`(quote ,(equal? c1 c2)))
(_ _ => `(equal? ,exp1 ,exp2))
))
(define (opt-if cnd exp1 exp2 vn vv)
(match
(cnd)
(('quote c) =>
(if c
(opt-exp exp1 vn vv)
(opt-exp exp2 vn vv)))
(_ =>
`(if ,cnd
,(opt-exp exp1 vn vv)
,(opt-exp exp2 vn vv)))
))
;;
;; Splits each parameter in the list "par*"
;; according to the corresponding type in "type*".
;;
(define (split-par* par* type*)
(select
(par* type*)
(() () => '(() . ()))
((par . r-par*) (type . r-type*) =>
(with* (( (names . exp*) (split-par* r-par* r-type*) )
( (names . exp) (split-par par type names) )
)
`(,names . (,exp . ,exp*))))
))
;;
;; Splits the parameter "par" into a list of new parameters.
;; The new parameters are appended to "names".
;;
(define (split-par par type names)
(if (or (eq? type 'absent)
(eq? type 'any))
`((,par . ,names) . ,par)
(with* (( maxnum (count-gaps type) )
( names (gen-new-names par 1 maxnum names) )
( (exp . _) (type-to-exp names type) )
)
`(,names . ,exp))))
;;
;; Counts "any"-s in "type".
;;
(define (count-gaps type)
(select
(type)
('absent => 1)
('any => 1)
(('atom _) => 0)
(('cons t1 t2) => (+ (count-gaps t1) (count-gaps t2)))
))
;;
;; Generates new variable names for the variable "var",
;; "num" being the number of variables to be generated.
;; The new variables are appended to "names".
;;
(define (gen-new-names var num maxnum names)
(if (> num maxnum)
names
`(,(string->symbol
(string-append
(symbol->string var)
"-$"
(number->string num)))
. ,(gen-new-names var (+ 1 num) maxnum names))))
;;
;; Converts the type "type" to an expression.
;; The gaps in "type" are filled with the first
;; variables from "names".
;; Returns a pair (exp . rest), where "exp" is
;; the expression generated and "rest" is the rest
;; of the list "names".
;;
(define (type-to-exp names type)
(select
(type)
('absent => names)
('any => names)
(('atom a) => `((quote ,a) . ,names))
(('cons t1 t2) =>
(with* (( (e1 . names) (type-to-exp names t1) )
( (e2 . names) (type-to-exp names t2) )
)
`(,(opt-cons e1 e2) . ,names)))
))
;;
;; Splits each argument in the list "arg*"
;; according to the corresponding type in "type*".
;;
(define (split-arg* arg* type*)
(select
(arg* type*)
(() () => '())
((arg . r-arg*) (type . r-type*) =>
(let ((exp* (split-arg* r-arg* r-type*)))
(split-arg arg type exp*)))
))
;;
;; Splits the argument "arg" into a list of new arguments.
;; The new arguments are appended to "exp*".
;;
(define (split-arg arg type exp*)
(select
(type)
('absent => `(,arg . ,exp*))
('any => `(,arg . ,exp*))
(('atom _) => exp*)
(('cons t1 t2) =>
(let ((exp* (split-arg (opt-cdr arg) t2 exp*)))
(split-arg (opt-car arg) t1 exp*)))
))
;;
;; Returns the type bound to "vname" in the environment
;; (vn*,vv*).
;;
(define (lookup-variable vname vn vv)
(select
(vn vv)
(() () => (error "Undefined variable" vname))
((vn . nrest) (vv . vrest) =>
(if (eq? vname vn)
vv
(lookup-variable vname nrest vrest)))
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; (uarps:optimize prog types) ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(map opt-fundef prog))