|
110 | 110 |
|
111 | 111 |
|
112 | 112 | ;; Reduce a static contract assuming that we trusted the current side |
113 | | -(define (trusted-side-reduce sc flat-sc?) |
| 113 | +(define ((make-trusted-side-reduce flat-sc?) sc) |
114 | 114 | (match sc |
115 | 115 | [(->/sc: mand-args opt-args mand-kw-args opt-kw-args rest-arg (list (any/sc:) ...)) |
116 | 116 | (function/sc #t mand-args opt-args mand-kw-args opt-kw-args rest-arg #f)] |
|
173 | 173 | ;; update-side : sc? weak-side? -> weak-side? |
174 | 174 | ;; Change the current side to something safe & strong-as-possible |
175 | 175 | ;; for optimizing the sub-contracts of the given `sc`. |
176 | | -(define (update-side sc side flat-sc?) |
| 176 | +(define ((make-update-side flat-sc?) sc side) |
177 | 177 | (match sc |
178 | 178 | [(or/sc: scs ...) |
179 | 179 | #:when (not (andmap flat-sc? scs)) |
|
289 | 289 | (sc-map sc trim)])) |
290 | 290 | (trim sc 'covariant)) |
291 | 291 |
|
| 292 | +(define (make-sc->kind recursive-kinds) |
| 293 | + (if recursive-kinds |
| 294 | + (λ (sc) |
| 295 | + (let loop ([sc sc]) |
| 296 | + (match sc |
| 297 | + [(recursive-sc _ _ body) |
| 298 | + (loop body)] |
| 299 | + [(or (recursive-sc-use id) |
| 300 | + (name/sc: id)) |
| 301 | + (hash-ref recursive-kinds id #f)] |
| 302 | + [_ |
| 303 | + (sc-terminal-kind sc)]))) |
| 304 | + sc-terminal-kind)) |
292 | 305 |
|
293 | 306 | ;; If we trust a specific side then we drop all contracts protecting that side. |
294 | 307 | (define (optimize sc #:trusted-positive [trusted-positive #f] #:trusted-negative [trusted-negative #f] #:recursive-kinds [recursive-kinds #f]) |
295 | | - (define sc->kind |
296 | | - (if (not recursive-kinds) |
297 | | - sc-terminal-kind |
298 | | - (λ (sc) |
299 | | - (let loop ([sc sc]) |
300 | | - (match sc |
301 | | - [(recursive-sc _ _ body) |
302 | | - (loop body)] |
303 | | - [(or (recursive-sc-use id) |
304 | | - (name/sc: id)) |
305 | | - (hash-ref recursive-kinds id #f)] |
306 | | - [_ |
307 | | - (sc-terminal-kind sc)]))))) |
308 | | - (define (flat-sc? sc) |
309 | | - (eq? 'flat (sc->kind sc))) |
| 308 | + (define flat-sc? |
| 309 | + (let ([sc->kind (make-sc->kind recursive-kinds)]) |
| 310 | + (λ (sc) (eq? 'flat (sc->kind sc))))) |
| 311 | + (define trusted-side-reduce (make-trusted-side-reduce flat-sc?)) |
| 312 | + (define update-side (make-update-side flat-sc?)) |
310 | 313 |
|
311 | 314 | ;; single-step: reduce and trusted-side-reduce if appropriate |
312 | 315 | (define (single-step sc maybe-weak-side) |
|
320 | 323 |
|
321 | 324 | (reduce |
322 | 325 | (if trusted |
323 | | - (trusted-side-reduce sc flat-sc?) |
| 326 | + (trusted-side-reduce sc) |
324 | 327 | sc))) |
325 | 328 |
|
326 | 329 | ;; full-pass: single-step at every static contract subpart |
327 | 330 | (define (full-pass sc) |
328 | 331 | (define ((recur side) sc variance) |
329 | 332 | (define curr-side (combine-variance side variance)) |
330 | | - (define next-side (update-side sc curr-side flat-sc?)) |
| 333 | + (define next-side (update-side sc curr-side)) |
331 | 334 | (single-step (sc-map sc (recur next-side)) curr-side)) |
332 | 335 | ((recur 'positive) sc 'covariant)) |
333 | 336 |
|
|
0 commit comments