Skip to content

Commit 6fa296c

Browse files
committed
add separate read and write types for Box types
1 parent 9b85c15 commit 6fa296c

File tree

13 files changed

+29
-29
lines changed

13 files changed

+29
-29
lines changed

typed-racket-lib/typed-racket/infer/infer-unit.rkt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -654,9 +654,9 @@
654654
;; vectors are invariant - generate constraints *both* ways
655655
[((Vector: e) (Vector: e*))
656656
(cg/inv e e*)]
657-
;; boxes are invariant - generate constraints *both* ways
658-
[((Box: e) (Box: e*))
659-
(cg/inv e e*)]
657+
;; boxes are contravariant for writing, covariant for reading
658+
[((Box: e-w e-r) (Box: e-w* e-r*))
659+
(% cset-meet (cg e-w* e-w) (cg e-r e-r*))]
660660
[((Weak-Box: e) (Weak-Box: e*))
661661
(cg/inv e e*)]
662662
[((MPair: s t) (MPair: s* t*))

typed-racket-lib/typed-racket/private/type-contract.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@
366366
[(Sequence: ts) (apply sequence/sc (map t->sc ts))]
367367
[(Vector: t) (vectorof/sc (t->sc/both t))]
368368
[(HeterogeneousVector: ts) (apply vector/sc (map t->sc/both ts))]
369-
[(Box: t) (box/sc (t->sc/both t))]
369+
[(Box: t1 t2) (box/sc (t->sc/neg t1) (t->sc t2))]
370370
[(Pair: t1 t2)
371371
(cons/sc (t->sc t1) (t->sc t2))]
372372
[(Async-Channel: t) (async-channel/sc (t->sc t))]

typed-racket-lib/typed-racket/rep/type-rep.rkt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@
179179
[#:fold-rhs (*HeterogeneousVector (map type-rec-id elems))])
180180

181181
;; elem is a Type
182-
(def-type Box ([elem Type/c])
183-
[#:frees (λ (f) (make-invariant (f elem)))]
182+
(def-type Box ([elem-w Type/c] [elem-r Type/c])
183+
[#:frees (λ (f) (combine-frees (list (flip-variances (f elem-w)) (f elem-r))))]
184184
[#:key 'box])
185185

186186
;; elem is a Type

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@
156156
((promise/sc (#:covariant)) (λ (x) (and/c (promise/c x) (not/c promise/name?))) #:chaperone)
157157
((syntax/sc (#:covariant #:flat)) syntax/c #:flat)
158158
((hash/sc (#:invariant #:flat) (#:invariant)) hash/c #:chaperone)
159-
((box/sc (#:invariant)) box/c #:chaperone)
159+
((box/sc (#:contravariant) (#:covariant)) box/c #:chaperone)
160160
((parameter/sc (#:contravariant) (#:covariant)) parameter/c #:chaperone)
161161
((sequence/sc . (#:covariant)) sequence/c #:impersonator)
162162
((channel/sc . (#:invariant)) channel/c #:chaperone)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
;; can call continuations and thus be useful even if they cannot return values.
3434
;[(vectorof/sc: (none/sc:)) empty-vector/sc]
3535
;[(vector/sc: sc1 ... (none/sc:) sc2 ...) none/sc]
36-
;[(box/sc: (none/sc:)) none/sc]
36+
;[(box/sc: (none/sc:) (none/sc:)) none/sc]
3737
;[(promise/sc: (none/sc:)) none/sc]
3838
;[(hash/sc: (none/sc:) value/sc) empty-hash/sc]
3939
;[(hash/sc: key/sc (none/sc:)) empty-hash/sc]
@@ -45,7 +45,7 @@
4545
[(vectorof/sc: (any/sc:)) vector?/sc]
4646
[(vector/sc: (and scs (any/sc:)) ...) (vector-length/sc (length scs))]
4747
[(set/sc: (any/sc:)) set?/sc]
48-
[(box/sc: (any/sc:)) box?/sc]
48+
[(box/sc: (any/sc:) (any/sc:)) box?/sc]
4949
[(syntax/sc: (any/sc:)) syntax?/sc]
5050
[(promise/sc: (any/sc:)) promise?/sc]
5151
[(hash/sc: (any/sc:) (any/sc:)) hash?/sc]

typed-racket-lib/typed-racket/typecheck/tc-expr-unit.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@
394394
(generalize (find-stx-type x #f))))])]
395395
[(box x)
396396
(match (and expected (resolve (restrict expected -BoxTop 'orig)))
397-
[(Box: t) (-box (check-below (find-stx-type x t) t))]
397+
[(Box: t-w t-r) (-box t-w (check-below (find-stx-type x t-r) t-r))]
398398
[_ (-box (generalize (find-stx-type x)))])]
399399
[(? hash? h)
400400
(match (and expected (resolve (restrict expected -HashTop 'orig)))

typed-racket-lib/typed-racket/types/abbrev.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
(define -App make-App)
5555
(define -mpair make-MPair)
5656
(define (-Param t1 [t2 t1]) (make-Param t1 t2))
57-
(define (-box t) (make-Box t))
57+
(define (-box t1 [t2 t1]) (make-Box t1 t2))
5858
(define -channel make-Channel)
5959
(define -async-channel make-Async-Channel)
6060
(define -thread-cell make-ThreadCell)

typed-racket-lib/typed-racket/types/printer.rkt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@
450450
[(arr: _ _ _ _ _) `(arr ,(arr->sexp type))]
451451
[(Vector: e) `(Vectorof ,(t->s e))]
452452
[(HeterogeneousVector: e) `(Vector ,@(map t->s e))]
453-
[(Box: e) `(Boxof ,(t->s e))]
453+
[(Box: e e) `(Boxof ,(t->s e))]
454454
[(Weak-Box: e) `(Weak-Boxof ,(t->s e))]
455455
[(Future: e) `(Futureof ,(t->s e))]
456456
[(Channel: e) `(Channelof ,(t->s e))]
@@ -500,7 +500,7 @@
500500
(Mu: y (Union: (list (F: x)
501501
(Pair: (F: x) (F: y)))))
502502
(Vector: (F: x))
503-
(Box: (F: x))))))
503+
(Box: (F: x) (F: x))))))
504504
'Syntax]
505505
[(Mu-name: name body) `(Rec ,name ,(t->s body))]
506506
[(B: idx) `(B ,idx)]

typed-racket-lib/typed-racket/types/structural.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
[Param (#:contra #:co)]
6262

6363
[Continuation-Mark-Keyof (#:inv)]
64-
[Box (#:inv)]
64+
[Box (#:contra #:co)]
6565
[Channel (#:inv)]
6666
[Async-Channel (#:inv)]
6767
[ThreadCell (#:inv)]

typed-racket-lib/typed-racket/types/subtype.rkt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,8 +531,8 @@
531531
[((Channel: t) (Evt: t*)) (subtype* A0 t t*)]
532532
[((Async-Channel: t) (Evt: t*)) (subtype* A0 t t*)]
533533
;; Invariant types
534-
[((Box: s) (Box: t)) (type-equiv? A0 s t)]
535-
[((Box: _) (BoxTop:)) A0]
534+
[((Box: s-w s-r) (Box: t-w t-r)) (subtypes* A0 (list t-w s-r) (list s-w t-r))]
535+
[((Box: _ _) (BoxTop:)) A0]
536536
[((Weak-Box: s) (Weak-Box: t)) (type-equiv? A0 s t)]
537537
[((Weak-Box: _) (Weak-BoxTop:)) A0]
538538
[((ThreadCell: s) (ThreadCell: t)) (type-equiv? A0 s t)]

0 commit comments

Comments
 (0)