Sealers/unsealer/brand in 3 lines of Racket

9 views
Skip to first unread message

Christopher Lemmer Webber

unread,
Mar 5, 2021, 2:45:00 PM3/5/21
to cap-...@googlegroups.com
I showed this off on a call today and it looked like some people hadn't
seen this approach before:

(define (new-sealer-triplet)
(struct sealed (obj))
(values sealed sealed-obj sealed?)) ; sealer unsealer brand?

Or, slightly more verbose, for clarity:

(define (new-sealer-triplet)
(struct sealed (obj))
(define (seal obj)
(sealed obj))
(define (unseal this-sealed-obj)
(sealed-obj this-sealed-obj))
(values seal unseal sealed?))

This is the approach we actually use in Goblins. No weak-map
required... take advantage of Racket's own c-list for dynamically
composed structures.

Just to clarify what's happening:
- We enclose over a dynamically new constructed "type" (here called
"structs" in Racket) with one slot, the sealed value
- We use the constructor as sealer
- We use the accessor as unsealer
- We use the type predicate as brand predicate

I borrowed this directly from Jonathan Rees' trick in W7:

https://github.com/jar398/w7/blob/master/w7-fun.scm#L37

Of course, this requires not having access to tools in the language that
permit you to destructure either closures or these kinds of records
generally. But in the restricted version of Racket/Scheme I'll be
passing around, that won't be done.

- Chris

PS: I actually don't love Racket's struct! It's unhygienic and dumps
variables directly into your socpe. (Where did sealed-obj and sealed?
come from?).
Reply all
Reply to author
Forward
0 new messages