[racket] users Digest, Vol 109, Issue 77

From: Moshe Deutsch (moshedeutsch115 at gmail.com)
Date: Mon Sep 29 16:40:43 EDT 2014

Please take me off the list

Thanks

On Mon, Sep 29, 2014 at 2:00 PM,  <users-request at racket-lang.org> wrote:
> Send users mailing list submissions to
>         users at racket-lang.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>         http://lists.racket-lang.org/users/listinfo
> or, via email, send a message with subject or body 'help' to
>         users-request at racket-lang.org
>
> You can reach the person managing the list at
>         users-owner at racket-lang.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of users digest..."
>
>
> [Racket Users list:
>  http://lists.racket-lang.org/users ]
>
>
> Today's Topics:
>
>    1. Cyclic data structures in Typed Racket (Konrad Hinsen)
>    2. Re: Cyclic data structures in Typed Racket (Sam Tobin-Hochstadt)
>    3. Re: Cyclic data structures in Typed Racket (Konrad Hinsen)
>    4. Re: Cyclic data structures in Typed Racket (Sam Tobin-Hochstadt)
>    5. Re: Cyclic data structures in Typed Racket (Benjamin Greenman)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Mon, 29 Sep 2014 18:10:29 +0200
> From: Konrad Hinsen <konrad.hinsen at fastmail.net>
> To: users at racket-lang.org
> Subject: [racket] Cyclic data structures in Typed Racket
> Message-ID:
>         <21545.33909.430329.240145 at Konrad-Hinsens-MacBook-Pro-2.local>
> Content-Type: text/plain; charset=us-ascii
>
> 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.
>
>
> ------------------------------
>
> Message: 2
> Date: Mon, 29 Sep 2014 12:17:29 -0400
> From: Sam Tobin-Hochstadt <samth at cs.indiana.edu>
> To: Konrad Hinsen <konrad.hinsen at fastmail.net>
> Cc: users <users at racket-lang.org>
> Subject: Re: [racket] Cyclic data structures in Typed Racket
> Message-ID:
>         <CAK=HD+Y2x3d1e=caisU9xayyFqRvwm+EHT+Rg66yDCpEAhNGtA at mail.gmail.com>
> Content-Type: text/plain; charset=UTF-8
>
> 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
>
>
> ------------------------------
>
> Message: 3
> Date: Mon, 29 Sep 2014 18:33:38 +0200
> From: Konrad Hinsen <konrad.hinsen at fastmail.net>
> To: Sam Tobin-Hochstadt <samth at cs.indiana.edu>
> Cc: users <users at racket-lang.org>
> Subject: Re: [racket] Cyclic data structures in Typed Racket
> Message-ID:
>         <21545.35298.582248.45625 at Konrad-Hinsens-MacBook-Pro-2.local>
> Content-Type: text/plain; charset=us-ascii
>
> Sam Tobin-Hochstadt writes:
>
>  > I recommend doing the mutation yourself, and not using `shared`.
>  > That's the most obvious solution.
>
> Not for me, unfortunately, but probably I am missing something
> obvious.
>
> Here's explicit mutation in untyped Racket:
>
>    #lang racket
>
>    (struct foo (x) #:mutable)
>    (struct bar (x))
>
>    (define f (foo (void)))
>    (define b (bar f))
>    (set-foo-x! f b)
>
>    (eq? (bar-x b) f)
>    (eq? (foo-x f) b)
>
> That works fine. Moving to Typed Racket, this is rejected by the type
> checker because (void) is of type Void.  Since I need a bar to make a
> foo and a foo to make a bar, I don't see how I can ever initialize my
> foo structure.
>
>  > 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.
>
> That makes sense, thanks for the explanation!
>
> Konrad.
>
>
> ------------------------------
>
> Message: 4
> Date: Mon, 29 Sep 2014 12:52:25 -0400
> From: Sam Tobin-Hochstadt <samth at cs.indiana.edu>
> To: Konrad Hinsen <konrad.hinsen at fastmail.net>
> Cc: users <users at racket-lang.org>
> Subject: Re: [racket] Cyclic data structures in Typed Racket
> Message-ID:
>         <CAK=HD+Ywfnfv-mjQPpo9GmOO8u+aV7f6ue-4motks_GkVnNRaQ at mail.gmail.com>
> Content-Type: text/plain; charset=UTF-8
>
> Ah, if you want to create truly cyclic structure with no base case
> like this, then you'll have to do more work. Either you'll need to
> expand the type of the `x` field in `foo` to allow an initial value,
> or you'll need to create a dummy `foo` with that extended type, and
> then copy the cyclic data once you've created it to a new version of
> `foo`, using hash tables to track cycles. That's basically how
> `shared` works, although I haven't tried implementing it in TR and it
> might not be possible.
>
> Sam
>
> On Mon, Sep 29, 2014 at 12:33 PM, Konrad Hinsen
> <konrad.hinsen at fastmail.net> wrote:
>> Sam Tobin-Hochstadt writes:
>>
>>  > I recommend doing the mutation yourself, and not using `shared`.
>>  > That's the most obvious solution.
>>
>> Not for me, unfortunately, but probably I am missing something
>> obvious.
>>
>> Here's explicit mutation in untyped Racket:
>>
>>    #lang racket
>>
>>    (struct foo (x) #:mutable)
>>    (struct bar (x))
>>
>>    (define f (foo (void)))
>>    (define b (bar f))
>>    (set-foo-x! f b)
>>
>>    (eq? (bar-x b) f)
>>    (eq? (foo-x f) b)
>>
>> That works fine. Moving to Typed Racket, this is rejected by the type
>> checker because (void) is of type Void.  Since I need a bar to make a
>> foo and a foo to make a bar, I don't see how I can ever initialize my
>> foo structure.
>>
>>  > 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.
>>
>> That makes sense, thanks for the explanation!
>>
>> Konrad.
>
>
> ------------------------------
>
> Message: 5
> Date: Mon, 29 Sep 2014 14:00:12 -0400
> From: Benjamin Greenman <blg59 at cornell.edu>
> To: Sam Tobin-Hochstadt <samth at cs.indiana.edu>
> Cc: users <users at racket-lang.org>
> Subject: Re: [racket] Cyclic data structures in Typed Racket
> Message-ID:
>         <CAAtAoRpHmBjH4FW1BwSOoDh41=m=BZt2q4wz9xdq+Wfj8RsRFQ at mail.gmail.com>
> Content-Type: text/plain; charset="utf-8"
>
> To be sure: Is there any TR analog to this OCaml code, initializing a
> cyclic, 3-element list? I've tried using letrec in TR, but the typechecker
> yells at me even if I annotate the a,b,c,d.
>
> # type 'a mylist = Nil | Cons of 'a * 'a mylist;;
> type 'a mylist = Nil | Cons of 'a * 'a mylist
> # let mylist =
>     let rec a = Cons(1, b)
>     and     b = Cons(2, c)
>     and     c = Cons(3, d)
>     and     d = Cons(4, a) in
>     a;;
> val mylist : int mylist = Cons (1, Cons (2, Cons (3, Cons (4, <cycle>))))
>
> On Mon, Sep 29, 2014 at 12:52 PM, Sam Tobin-Hochstadt <samth at cs.indiana.edu>
> wrote:
>
>> Ah, if you want to create truly cyclic structure with no base case
>> like this, then you'll have to do more work. Either you'll need to
>> expand the type of the `x` field in `foo` to allow an initial value,
>> or you'll need to create a dummy `foo` with that extended type, and
>> then copy the cyclic data once you've created it to a new version of
>> `foo`, using hash tables to track cycles. That's basically how
>> `shared` works, although I haven't tried implementing it in TR and it
>> might not be possible.
>>
>> Sam
>>
>> On Mon, Sep 29, 2014 at 12:33 PM, Konrad Hinsen
>> <konrad.hinsen at fastmail.net> wrote:
>> > Sam Tobin-Hochstadt writes:
>> >
>> >  > I recommend doing the mutation yourself, and not using `shared`.
>> >  > That's the most obvious solution.
>> >
>> > Not for me, unfortunately, but probably I am missing something
>> > obvious.
>> >
>> > Here's explicit mutation in untyped Racket:
>> >
>> >    #lang racket
>> >
>> >    (struct foo (x) #:mutable)
>> >    (struct bar (x))
>> >
>> >    (define f (foo (void)))
>> >    (define b (bar f))
>> >    (set-foo-x! f b)
>> >
>> >    (eq? (bar-x b) f)
>> >    (eq? (foo-x f) b)
>> >
>> > That works fine. Moving to Typed Racket, this is rejected by the type
>> > checker because (void) is of type Void.  Since I need a bar to make a
>> > foo and a foo to make a bar, I don't see how I can ever initialize my
>> > foo structure.
>> >
>> >  > 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.
>> >
>> > That makes sense, thanks for the explanation!
>> >
>> > Konrad.
>> ____________________
>>   Racket Users list:
>>   http://lists.racket-lang.org/users
>>
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <http://lists.racket-lang.org/users/archive/attachments/20140929/152cdf29/attachment.html>
>
> End of users Digest, Vol 109, Issue 77
> **************************************

Posted on the users mailing list.