[racket] Cyclic data structures in Typed Racket

From: Sam Tobin-Hochstadt (samth at cs.indiana.edu)
Date: Mon Sep 29 12:17:29 EDT 2014

I recommend doing the mutation yourself, and not using `shared`.
That's the most obvious solution.

The reason that your `require/typed` doesn't work is that it creates
new versions of the placeholder functions, but those aren't the ones
that `shared` uses internally, so Typed Racket doesn't use the types
you've given.

Note also that the types you gave aren't safe -- the way shared uses
placeholders expects particular types for particular ones (this would
result in a contract error at runtime if `shared` used your bindings).

Sam

On Mon, Sep 29, 2014 at 12:10 PM, Konrad Hinsen
<konrad.hinsen at fastmail.net> wrote:
> Hi everyone,
>
> I am trying to port code creating a cyclic data structure to Typed
> Racket, but notice that this is not as straightforward as I expected.
>
> Here is the untyped code I start from:
>
>    #lang racket
>
>    (struct foo (x) #:mutable)
>    (struct bar (x))
>
>    (define-values (f b)
>      (shared ([f (foo b)]
>               [b (bar f)])
>              (values f b)))
>
>    (eq? (bar-x b) f)
>    (eq? (foo-x f) b)
>
> Switching to Typed Racket and annotating the struct definitions, I get
> an error message saying that make-placeholder and placeholder-set!
> from racket/base are lacking type annotations. I also get the recommendation
> to use require/typed for importing them. OK, let's try:
>
>    #lang typed/racket
>
>    (require/typed racket/base
>      [#:opaque Placeholder placeholder?]
>      [make-placeholder (-> Any Placeholder)]
>      [placeholder-set! (-> Placeholder Any Void)])
>
>    (struct foo ([x : bar]) #:mutable)
>    (struct bar ([x : foo]))
>
>    (define-values (f b)
>      (shared ([f (foo b)]
>               [b (bar f)])
>              (values f b)))
>
>    (eq? (bar-x b) f)
>    (eq? (foo-x f) b)
>
> Same error message... which is not really surprising. I suppose typed/racket
> already requires racket/base, so my additional require/typed has no effect.
> But how can I fix this? I don't see any way to exclude items from typed/racket.
>
> Konrad.
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users

Posted on the users mailing list.