Skip to content

Commit fc15feb

Browse files
committed
test: name/sc remembers if flat or not
This is slower than previous commit, 31s
1 parent 998ddbf commit fc15feb

File tree

3 files changed

+38
-31
lines changed

3 files changed

+38
-31
lines changed

typed-racket-lib/typed-racket/static-contracts/combinators/name.rkt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
(provide with-new-name-tables
2424
name/sc:
2525
lookup-name-defined
26-
set-name-defined)
26+
set-name-defined
27+
set-flat-names!)
2728

2829
(provide/cond-contract
2930
[get-all-name-defs
@@ -41,6 +42,12 @@
4142
(define name-sc-table (make-parameter (make-hash)))
4243
(define name-defs-table (make-parameter (make-hash)))
4344

45+
(define (set-flat-names! gen-names)
46+
(for* ([ns (in-hash-values (name-sc-table))]
47+
[n (in-list ns)]
48+
#:when (member (name-combinator-gen-name n) gen-names))
49+
(set-name-combinator-flat! n #true)))
50+
4451
;; Use this table to track whether a contract has already been
4552
;; generated for this name type yet. Stores booleans.
4653
(define name-defined-table (make-parameter (make-free-id-table)))
@@ -91,7 +98,7 @@
9198
type
9299
(list typed-sc untyped-sc both-sc)))
93100

94-
(struct name-combinator combinator (gen-name)
101+
(struct name-combinator combinator (gen-name [flat #:auto #:mutable])
95102
#:transparent
96103
#:property prop:combinator-name "name/sc"
97104
#:methods gen:sc
@@ -101,8 +108,13 @@
101108
(define (sc->contract v f)
102109
(name-combinator-gen-name v))
103110
(define (sc->constraints v f)
104-
(variable-contract-restrict (name-combinator-gen-name v)))])
111+
(variable-contract-restrict (name-combinator-gen-name v)))
112+
(define (sc-terminal-kind v)
113+
(if (name-combinator-flat v)
114+
'flat
115+
#f))])
105116

106117
(define-match-expander name/sc:
107118
(syntax-parser
108-
[(_ var) #'(name-combinator _ var)]))
119+
[(_ var) #'(name-combinator _ var _)]))
120+

typed-racket-lib/typed-racket/static-contracts/instantiate.rkt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,23 @@
3737
compute-recursive-kinds
3838
instantiate/inner))
3939

40+
(define (name/sc? v)
41+
(printf "nmae? ~a~n" v)
42+
(match v
43+
[(name/sc: _) #true]
44+
[_ #false]))
45+
4046
(define (instantiate/optimize sc fail kind #:cache cache #:trusted-positive trusted-positive #:trusted-negative trusted-negative)
4147
(define recursive-kinds
4248
(with-handlers [(exn:fail:constraint-failure?
4349
(lambda (exn) (fail #:reason (exn:fail:constraint-failure-reason exn))))]
4450
(compute-recursive-kinds
4551
(contract-restrict-recursive-values (compute-constraints sc kind)))))
46-
(define sc/opt (optimize sc #:trusted-positive trusted-positive #:trusted-negative trusted-negative #:recursive-kinds recursive-kinds))
52+
(set-flat-names!
53+
(for/list ([(k v) (in-hash recursive-kinds)]
54+
#:when (eq? 'flat v))
55+
k))
56+
(define sc/opt (optimize sc #:trusted-positive trusted-positive #:trusted-negative trusted-negative))
4757
(instantiate sc/opt fail kind #:cache cache #:recursive-kinds recursive-kinds))
4858

4959
;; kind is the greatest kind of contract that is supported, if a greater kind would be produced the

typed-racket-lib/typed-racket/static-contracts/optimize.rkt

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,15 @@
110110

111111

112112
;; Reduce a static contract assuming that we trusted the current side
113-
(define (trusted-side-reduce sc flat?)
113+
(define (trusted-side-reduce sc)
114114
(match sc
115115
[(->/sc: mand-args opt-args mand-kw-args opt-kw-args rest-arg (list (any/sc:) ...))
116116
(function/sc #t mand-args opt-args mand-kw-args opt-kw-args rest-arg #f)]
117117
[(arr/sc: args rest (list (any/sc:) ...))
118118
(arr/sc args rest #f)]
119119
[(none/sc:) any/sc]
120-
[(or/sc: (? flat?) ...) any/sc]
121-
[(? flat?) any/sc]
120+
[(or/sc: (? flat-terminal-kind?) ...) any/sc]
121+
[(? flat-terminal-kind?) any/sc]
122122
[(syntax/sc: (? recursive-sc?))
123123
;;bg; _temporary_ case to allow contracts from the `Syntax` type.
124124
;; This is temporary until TR has types for immutable-vector
@@ -176,13 +176,13 @@
176176
;; update-side : sc? weak-side? -> weak-side?
177177
;; Change the current side to something safe & strong-as-possible
178178
;; for optimizing the sub-contracts of the given `sc`.
179-
(define (update-side sc side flat?)
179+
(define (update-side sc side)
180180
(match sc
181181
[(or/sc: scs ...)
182-
#:when (not (andmap flat? scs))
182+
#:when (not (andmap flat-terminal-kind? scs))
183183
(weaken-side side)]
184184
[_
185-
#:when (guarded-sc? sc flat?)
185+
#:when (guarded-sc? sc)
186186
(strengthen-side side)]
187187
[_
188188
;; Keep same side by default.
@@ -193,9 +193,9 @@
193193
;; guarded-sc? : sc? -> boolean?
194194
;; Returns #true if the given static contract represents a type with a "real"
195195
;; type constructor. E.g. list/sc is "real" and or/sc is not.
196-
(define (guarded-sc? sc flat?)
196+
(define (guarded-sc? sc)
197197
(match sc
198-
[(or (? flat?)
198+
[(or (? flat-terminal-kind?)
199199
(->/sc: _ _ _ _ _ _)
200200
(arr/sc: _ _ _)
201201
(async-channel/sc: _)
@@ -295,22 +295,7 @@
295295

296296

297297
;; If we trust a specific side then we drop all contracts protecting that side.
298-
(define (optimize sc #:trusted-positive [trusted-positive #f] #:trusted-negative [trusted-negative #f] #:recursive-kinds [recursive-kinds #f])
299-
(define flat?/sc
300-
(let* ([flat-names
301-
(and recursive-kinds
302-
(for/set ([(k v) (in-hash recursive-kinds)]
303-
#:when (eq? 'flat v))
304-
k))])
305-
(if flat-names
306-
(λ (sc)
307-
(match sc
308-
[(name/sc: name)
309-
(set-member? flat-names name)]
310-
[_
311-
(flat-terminal-kind? sc)]))
312-
flat-terminal-kind?)))
313-
298+
(define (optimize sc #:trusted-positive [trusted-positive #f] #:trusted-negative [trusted-negative #f])
314299
;; single-step: reduce and trusted-side-reduce if appropriate
315300
(define (single-step sc maybe-weak-side)
316301
(define trusted
@@ -323,14 +308,14 @@
323308

324309
(reduce
325310
(if trusted
326-
(trusted-side-reduce sc flat?/sc)
311+
(trusted-side-reduce sc)
327312
sc)))
328313

329314
;; full-pass: single-step at every static contract subpart
330315
(define (full-pass sc)
331316
(define ((recur side) sc variance)
332317
(define curr-side (combine-variance side variance))
333-
(define next-side (update-side sc curr-side flat?/sc))
318+
(define next-side (update-side sc curr-side))
334319
(single-step (sc-map sc (recur next-side)) curr-side))
335320
((recur 'positive) sc 'covariant))
336321

0 commit comments

Comments
 (0)