-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcallables.scm
434 lines (387 loc) · 15.2 KB
/
callables.scm
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
;; callables.scm
;; Definitions of methods, generics, signatures, and the functions that call them.
(define (callable? v)
(or (generic? v) (method? v) (procedure? v)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; GENERIC FUNCTIONS
(define-callable-record-type generic call-generic :generic
(really-make-generic name methods composer add-method-check/f)
generic?
(name generic-name set-generic-name!)
(methods generic-methods set-generic-methods!)
;; composer : (generic list-of-applicable-methods, argvals) =>
;; call-context
(composer generic-composer set-generic-composer!)
(add-method-check/f generic-add-method-check/f
set-generic-add-method-check/f!))
(define (call-generic callable-gf real-gf . args)
;; (1) find applicable methods
(let ((app-ms
(filter (rcurry standard-method-applicable? args)
(generic-methods real-gf))))
;; (2) compose applicable methods
(let ((context
((generic-composer real-gf) real-gf app-ms args)))
;; (3) initiate new call context
(let-fluid *call-context*
context
(lambda ()
;; (4) and go
((call-context-executor context)))))))
(define-record-discloser :generic
(lambda (v) `(generic ,(generic-name v))))
(define (make-generic . methods)
(apply make-named-generic "anon" methods))
(define (make-named-generic name . methods)
(let ((gf (really-make-generic name methods
primary-composer standard-add-method-check)))
(for-each (lambda (m)
(set-method-generic/f! m gf))
methods)
gf))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; METHODS
;;;
(define-callable-record-type method call-method :method
(make-method name args-type result-type callable generic/f)
method?
(name method-name set-method-name!)
(args-type method-args-type)
(result-type method-result-type)
(callable method-callable)
;; currently a method can be in at most one generic
(generic/f method-generic/f set-method-generic/f!))
(define (call-method callable-m real-m . args)
(dbg 'calls " call-method - checking sig...")
;; TO DO: many times, this check is redundant -- if the method was put
;; into an effective fn., it has already shown its applicability!
(check-applicable! (method-args-type real-m) args) ; throws error if fails
(dbg 'calls " sig. checked - doing apply")
(set-call-context-callable! (fluid *call-context*) callable-m)
(if (and (method-result-type real-m)
(not (top-type? (method-result-type real-m))))
(check-type! (apply (method-callable real-m) args)
(method-result-type real-m))
(apply (method-callable real-m) args)))
(define-record-discloser :method
(lambda (v) `(method ,(method-name v) ,(method-args-type v)
=> ,(method-result-type v))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; CALL CONTEXT (reflective interface to dynamic state)
;;;
(define-record-type call-context :call-context
(make-call-context generic chain next callable argvals executor)
(generic call-context-generic)
(chain call-context-chain set-call-context-chain!)
(next call-context-next set-call-context-next!)
(callable call-context-callable set-call-context-callable!)
(argvals call-context-argvals)
(executor call-context-executor set-call-context-executor!))
(define *call-context* (make-fluid (make-call-context #f #f #f #f #f #f)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; METHOD COMPOSITION
;;;
;; Choose a single most applicable method, and leave the rest for next-ms
;; Throw exception if not a single most applicable method.
(define (primary-composer generic app-ms vals)
(if (null? app-ms)
(error "No applicable method, generic=~a, vals=~a" generic vals)
(let ((mams (standard-method-selector app-ms vals)))
(if (> (length mams) 1)
(error "Ambiguous: ~a on values ~a" mams vals))
;; here we know mams is a list of length 1
(make-call-context
generic mams ; generic, chain
(remove (curry eq? (car mams)) app-ms) ; next
#f vals ; callable, argvals
(lambda () (apply (car mams) vals)))))) ; executor
;; chain in increasing order
(define (before-composer generic app-ms vals)
(let ((sorted-ms
(reverse ; want general to specific
(bubble-sort
app-ms
(lambda (m1 m2)
(subtype? (method-args-type m1)
(method-args-type m2)))))))
(chain-composer generic sorted-ms vals)))
;; chain in decreasing order
(define (after-composer generic app-ms vals)
(let ((sorted-ms
(bubble-sort
app-ms
(lambda (m1 m2)
(subtype? (method-args-type m1)
(method-args-type m2))))))
(chain-composer generic sorted-ms vals)))
;; Composer for a generic with before, after, around, and primary
;; generic functions.
;; Note that currently, this is identical to after-composer (!)
;; but needs to be a separate object because it's used as the tag
;; to decide if a generic has been "two-leveled".
(define (method-combination-composer . args)
(apply after-composer args))
; TO DO: *** Concern: next-method from last before method should go to primary ***
(define *return-value* (make-fluid *undefined*))
;; simply execute the applicable methods in order
;; Note that the fluid variable *return-value*
;; is bound during execution of an <after> method, if any.
(define (chain-composer generic app-ms vals)
(make-call-context
generic app-ms ; generic, chain
'() #f vals ; next, callable, args
(lambda () ; executor
(fold (lambda (m-todo return-val)
(if (eq? <after> (method-args-type m-todo))
(let-fluid *return-value*
return-val
(lambda ()
(apply m-todo vals)
return-val))
(apply m-todo vals)))
*undefined*
app-ms))))
;; TO DO: call-next-method is too expensive.
;; keeps the current call context, but mutates chain and next
(define (call-next-method)
(let ((the-context (fluid *call-context*)))
(cond
;; if we're in the midst of a chain, call next method in chain
;; ** for now, that means recompose to a new effective fn. **
((memq (call-context-callable the-context)
(call-context-chain the-context))
=> (lambda (chain-rest)
;; recompose effective function
(let ((new-context
((generic-composer (call-context-generic the-context))
(call-context-generic the-context)
;; using rest of chain and all next methods
(append (cdr chain-rest)
(call-context-next the-context))
(call-context-argvals the-context))))
;; keep the current context -- hack it
(set-call-context-chain! ; chain
the-context (call-context-chain new-context))
(set-call-context-next! ; next
the-context (call-context-next new-context))
;; callable is set by the individual method (in call-method)
;; argvals stay the same
(set-call-context-executor! ; executor
the-context (call-context-executor new-context))
;; now go
((call-context-executor new-context)))))
(else
(error "call-next-method called while not in a chain")))))
(define (unchecked-call callable args)
(dbg 'calls "unchecked-call ~a(args: ~a)" callable args)
(cond
((method? callable)
(unchecked-call (method-callable callable) args))
((procedure? callable)
(apply callable args))
(else
(error "Invalid callable in unchecked-call: ~a" callable))))
;; Returns most applicable methods (more than one if ambiguous).
(define (standard-method-selector app-meths vals)
;; Find most applicable (leaving all others unsorted).
;; -- uses only the methods' signatures -- not the actual arg.s.
(fold
;; mams holds candidates for mam (all elts mutually ambiguous)
(lambda (m mams)
(let ((m-type (method-args-type m)))
;; Cannot have situation where both m is <= some method m' in mams
;; AND m is >= some other function m'' in mams -- that would imply that
;; m' and m'' are comparable and therefore not mutually ambiguous.
;; Also, better not have case that arg-types(m) = arg-types(m') as that would
;; be duplicate methods problem.
(cond
;; if m >= any m' in mams, m is not a mam candidate
((any (rcurry subtype? m-type)
(map method-args-type mams))
mams)
;; if m < any m' in mams, replace all such m' with m
((any (curry subtype? m-type)
(map method-args-type mams))
(cons m
(filter
(lambda (m1)
(not (subtype?
m-type (method-args-type m1))))
mams)))
;; otherwise, must be incomparable with all elts of mam, so add m
(else
(cons m mams)))))
(list (car app-meths))
(cdr app-meths)))
(define (standard-method-applicable? m vals)
(isa? vals (method-args-type m)))
;; errs if duplicate
(define (standard-add-method-check m gf)
(if (find (lambda (m1)
(type-equal? (method-args-type m1)
(method-args-type m)))
(generic-methods gf))
(error "Adding duplicate method - use replace-method. ~a, ~a"
gf m)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; METHOD COMBINATION
;;
;; before < primary < after
;;
(define <after> (const #t))
(define <primary> (and? <after> (const #t)))
(define <before> (and? <primary> (const #t)))
;; take plain old generic, with only primary methods, and
;; replace generic-methods with 3 new "methods", before-method, after-method,
;; and primary-method. Each new method has a generic as its callable, and
;; each of these generics has a different composer fn.
;; change composer for original generic to method-combination-composer
(define (make-generic-two-level! gf)
;; does this generic need to be abstracted to a two-level generic?
(if (not (or (eq? (generic-composer gf) method-combination-composer)
(eq? (generic-composer gf) around-composer)))
(begin
(format #t "making generic ~a two-level~%" gf)
(let* ((primary-generic
;; updates method-generic/f ptr.s to new gf
(apply make-named-generic
(format #f "primary generic for ~a" (generic-name gf))
(generic-methods gf)))
(primary-method
(make-method (format #f "primary method for ~a"
(generic-name gf))
<primary> <top> ; args-type, result-type
primary-generic gf)) ; callable, generic/f
(before-generic
;; no methods yet
(really-make-generic
(format #f "before generic for ~a" (generic-name gf))
'() before-composer ; methods, composer
(const #t))) ; add-method: allow dupes
(before-method
(make-method (format #f "before method for ~a"
(generic-name gf))
<before> <top> ; args-type, result-type
before-generic gf)) ; callable, generic/f
(after-generic
;; no methods yet
(really-make-generic
(format #f "after generic for ~a" (generic-name gf))
'() after-composer ; methods, composer
(const #t))) ; add-method: allow dupes
(after-method
(make-method (format #f "after method for ~a"
(generic-name gf))
<after> <top> ; args-type, result-type
after-generic gf))) ; callable, generic/f
(set-generic-methods! gf (list before-method primary-method after-method))
(set-generic-composer! gf method-combination-composer)))))
(define (find-hidden-generic gf ref-fn label)
(cond
((eq? (generic-composer gf) around-composer)
(cond ((find (lambda (m) (eq? (method-args-type m) <default-around-type>))
(generic-methods gf))
=> (lambda (m) (find-hidden-generic (method-callable m) ref-fn label)))
(else
(error "could not find default around method, finding ~a generic"
label))))
((eq? (generic-composer gf) method-combination-composer)
(method-callable (ref-fn (generic-methods gf))))
(else
(if (eq? label 'primary)
gf
(error "no ~a - generic not abstracted" label)))))
(define (before-generic gf) (find-hidden-generic gf car 'before))
(define (primary-generic gf) (find-hidden-generic gf cadr 'primary))
(define (after-generic gf) (find-hidden-generic gf caddr 'after))
(define (add-primary-method gf m)
(let ((primary-gf (primary-generic gf)))
(if (generic-add-method-check/f primary-gf)
((generic-add-method-check/f primary-gf) m gf))
(set-method-generic/f! m primary-gf)
(set-generic-methods!
primary-gf (cons m (generic-methods primary-gf)))))
;; add-method is synonym for add-primary-method
(define add-method add-primary-method)
(define (add-method* gf . ms)
(for-each (curry add-primary-method gf)
ms))
(define (remove-primary-method gf sig)
(let ((primary-gf (primary-generic gf)))
(cond
((find (lambda (m1)
(type-equal? sig (method-args-type m1)))
(generic-methods primary-gf))
=> (lambda (m)
(set-generic-methods!
primary-gf
(delete! m (generic-methods primary-gf)))))
(else
(error "Could not find method matching ~a for generic ~a" sig gf)))))
(define (replace-primary-method gf sig m)
(remove-primary-method gf sig)
(add-primary-method gf m))
(define replace-method replace-primary-method)
;; remove-method is synonym for remove-primary-method
(define remove-method remove-primary-method)
(define (add-before-method gf m)
(make-generic-two-level! gf)
(add-primary-method (before-generic gf)
m))
(define (remove-before-method gf sig)
(remove-primary-method (before-generic gf) sig))
(define (add-after-method gf m)
(make-generic-two-level! gf)
(add-primary-method (after-generic gf) m))
(define (remove-after-method gf sig)
(remove-primary-method (after-generic gf) sig))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; AROUND METHODS (are special)
;;
;; Choose a single most applicable method, and leave the rest for next-ms
;; Throw exception if not a single most applicable method.
;; around-composer is same as primary-composer, but needs separate identity.
(define (around-composer generic app-ms vals)
(if (null? app-ms)
(error "No applicable around method, generic=~a, vals=~a" generic vals)
(let ((mams (standard-method-selector app-ms vals)))
(if (> (length mams) 1)
(error "Ambiguous: ~a on values ~a" mams vals))
;; here we know mams is a list of length 1
(make-call-context
generic mams ; generic, chain
(remove (curry eq? (car mams)) app-ms) ; next
#f vals ; callable, argvals
(lambda () (apply (car mams) vals)))))) ; executor
(define <default-around-type> (or? <top> (const #t))) ; will be super of just <top>
;; take a "two-level" generic and turn it into a three-level generic.
(define (make-generic-arounded! gf)
(make-generic-two-level! gf)
(if (not (eq? (generic-composer gf) around-composer))
(begin
(format #t "making generic aroundable.~%")
;; default-around will call before-primary-around methods as usual
(let* ((default-generic ; callable of default around method
(really-make-generic
(format #f "default around generic for ~a" (generic-name gf))
(generic-methods gf)
method-combination-composer
standard-add-method-check))
(default-around-method
(make-method
(format #f "default around method for ~a" (generic-name gf))
<default-around-type> <top> ; args-type, result-type
default-generic gf))) ; callable, generic/f
(set-generic-methods! gf (list default-around-method))
(set-generic-composer! gf around-composer)))))
(define (add-around-method gf m)
(make-generic-arounded! gf)
(set-method-generic/f! m gf)
(set-generic-methods! gf (cons m (generic-methods gf))))
;; eof