Skip to content

Commit aa9f5a3

Browse files
committed
add Boxof/Read, Boxof/Write, and Boxof/Write-Read
1 parent 7afbe26 commit aa9f5a3

File tree

5 files changed

+51
-8
lines changed

5 files changed

+51
-8
lines changed

typed-racket-doc/typed-racket/scribblings/reference/types.scrbl

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,41 @@ corresponding to @racket[trest], where @racket[bound]
354354

355355
@ex[(box "hello world")]
356356

357+
@defform[(Boxof/Read t)]{
358+
A @rtech{box} of @racket[t] that only supports read-only operations
359+
such as @racket[unbox]. A @racket[(Boxof/Read t)] is a subtype of
360+
@racket[(Boxof t)], and a @racket[(Boxof/Read a)] is a subtype of
361+
@racket[(Boxof/Read b)] if @racket[a] is a subtype of @racket[b]. It
362+
is equivalent to @racket[(Boxof/Write-Read Nothing t)].}
363+
364+
@ex[(box-immutable "hello world")
365+
(code:comment "though this does not necessarily imply immutability:")
366+
(define b : (Boxof Integer) (box 3))
367+
(define b-read-only : (Boxof/Read Integer) b)
368+
(set-box! b-read-only 5)
369+
(unbox b-read-only)
370+
(set-box! b 5)
371+
(unbox b-read-only)]
372+
373+
@defform[(Boxof/Write t)]{
374+
A @rtech{box} that could contain anything, but can only take
375+
@racket[t] for write operations such as @racket[set-box!].
376+
A @racket[(Boxof/Write t)] is a supertype of @racket[(Boxof t)], and
377+
it is equivalent to @racket[(Boxof/Write-Read t Any)].}
378+
379+
@defform[(Boxof/Write-Read write-t read-t)]{
380+
A @rtech{box} that supports write operations like @racket[set-box!]
381+
taking @racket[write-t], and read operations like @racket[unbox]
382+
returning @racket[read-t]. For this type to make sense,
383+
@racket[write-t] should be a subtype of @racket[read-t].
384+
385+
The type @racket[(Boxof t)] is equivalent to
386+
@racket[(Boxof/Write-Read t t)], the type
387+
@racket[(Boxof/Read t)] is equivalent to
388+
@racket[(Boxof/Write-Read Nothing t)], and the type
389+
@racket[(Boxof/Write t)] is equivalent to
390+
@racket[(Boxof/Write-Read t Any)].}
391+
357392
@defidform[BoxTop]{is the type of a @rtech{box} with an unknown element
358393
type and is the supertype of all box types. Only read-only box operations
359394
(e.g. @racket[unbox]) are allowed on values of this type. This type

typed-racket-lib/typed-racket/base-env/base-env.rkt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"base-structs.rkt"
2222
racket/file
2323
(only-in racket/private/pre-base new-apply-proc)
24-
(only-in (types abbrev) [-Boolean B] [-Symbol Sym] -Flat)
24+
(only-in (types abbrev) [-Boolean B] [-Symbol Sym] -Flat -Boxof/Read -Boxof/Write)
2525
(only-in (types numeric-tower) [-Number N])
2626
(only-in (rep type-rep)
2727
make-ClassTop
@@ -866,19 +866,19 @@
866866

867867
;; Section 4.12 (Boxes)
868868
[box (-poly (a) (a . -> . (-box a)))]
869-
[box-immutable (-poly (a) (a . -> . (-box a)))]
869+
[box-immutable (-poly (a) (a . -> . (-Boxof/Read a)))]
870870
[unbox (-poly (a) (cl->*
871-
((-box a) . -> . a)
871+
((-Boxof/Read a) . -> . a)
872872
((make-BoxTop) . -> . Univ)))]
873-
[set-box! (-poly (a) ((-box a) a . -> . -Void))]
873+
[set-box! (-poly (a) ((-Boxof/Write a) a . -> . -Void))]
874874
[unsafe-unbox (-poly (a) (cl->*
875-
((-box a) . -> . a)
875+
((-Boxof/Read a) . -> . a)
876876
((make-BoxTop) . -> . Univ)))]
877-
[unsafe-set-box! (-poly (a) ((-box a) a . -> . -Void))]
877+
[unsafe-set-box! (-poly (a) ((-Boxof/Write a) a . -> . -Void))]
878878
[unsafe-unbox* (-poly (a) (cl->*
879-
((-box a) . -> . a)
879+
((-Boxof/Read a) . -> . a)
880880
((make-BoxTop) . -> . Univ)))]
881-
[unsafe-set-box*! (-poly (a) ((-box a) a . -> . -Void))]
881+
[unsafe-set-box*! (-poly (a) ((-Boxof/Write a) a . -> . -Void))]
882882
[box? (make-pred-ty (make-BoxTop))]
883883

884884
;; Section 4.13 (Hash Tables)

typed-racket-lib/typed-racket/base-env/base-types.rkt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@
172172
[Promise (-poly (a) (-Promise a))]
173173
[Pair (-poly (a b) (-pair a b))]
174174
[Boxof (-poly (a) (-box a))]
175+
[Boxof/Read (-poly (r) (-Boxof/Read r))]
176+
[Boxof/Write (-poly (w) (-Boxof/Write w))]
177+
[Boxof/Write-Read (-poly (w r) (-box w r))]
175178
[Weak-Boxof (-poly (a) (-weak-box a))]
176179
[Channelof (-poly (a) (make-Channel a))]
177180
[Async-Channelof (-poly (a) (make-Async-Channel a))]

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
(define -mpair make-MPair)
5656
(define (-Param t1 [t2 t1]) (make-Param t1 t2))
5757
(define (-box t1 [t2 t1]) (make-Box t1 t2))
58+
(define (-Boxof/Read t) (-box -Bottom t))
59+
(define (-Boxof/Write t) (-box t Univ))
5860
(define -channel make-Channel)
5961
(define -async-channel make-Async-Channel)
6062
(define -thread-cell make-ThreadCell)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,9 @@
451451
[(Vector: e) `(Vectorof ,(t->s e))]
452452
[(HeterogeneousVector: e) `(Vector ,@(map t->s e))]
453453
[(Box: e e) `(Boxof ,(t->s e))]
454+
[(Box: (Union: '()) e) `(Boxof/Read ,(t->s e))]
455+
[(Box: e (Univ:)) `(Boxof/Write ,(t->s e))]
456+
[(Box: e1 e2) `(Boxof/Write-Read ,(t->s e1) ,(t->s e2))]
454457
[(Weak-Box: e) `(Weak-Boxof ,(t->s e))]
455458
[(Future: e) `(Futureof ,(t->s e))]
456459
[(Channel: e) `(Channelof ,(t->s e))]

0 commit comments

Comments
 (0)