[racket] users Digest, Vol 99, Issue 77

From: Emina Torlak (emina at eecs.berkeley.edu)
Date: Wed Nov 20 12:50:24 EST 2013

I think Sam's solution is sufficient for Typed Racket because the type
computation is purely static.  The problem in my case is that I need a way
to identify the dynamic locations of (statically identified) variables that
get created during execution.


On Wed, Nov 20, 2013 at 5:34 AM, William Cushing
<william.cushing at gmail.com>wrote:

> For emina's problem, shouldn't the implementation of typed/racket have a
> clue?
>
>
> On Wed, Nov 20, 2013 at 5:19 AM, <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. Why does this tail recursive list length function fail at cdr
>>       l at end of list? (Bo Gus)
>>    2. Re: Why does this tail recursive list length function fail at
>>       cdr l at end of list? (Pierpaolo Bernardi)
>>    3. Re: Why does this tail recursive list length function fail at
>>       cdr l at end of list? (Bo Gus)
>>    4. Re: Why does this tail recursive list length function fail at
>>       cdr l at end of list? (Daniel Prager)
>>    5. Re: Why does this tail recursive list length function fail at
>>       cdr l at end of list? (Daniel Prager)
>>    6. Re: obtaining the location of an identifier (Robby Findler)
>>
>>
>> ---------- Forwarded message ----------
>> From: Bo Gus <forumangus at gmail.com>
>> To: Racket Newsgroup <users at racket-lang.org>
>> Cc:
>> Date: Wed, 20 Nov 2013 11:09:28 +0000
>> Subject: [racket] Why does this tail recursive list length function fail
>> at cdr l at end of list?
>> My tail recursive implementation of length is like this:
>>
>> (define (length2 l)
>>   (define (l-iter l count)
>>     (if (null? 1) count
>>         (l-iter (cdr l) (+ count 1))))
>>   (l-iter l 0))
>>
>> If I call (length2 '(1 2 3)) and step through the code in racket, count
>> increments to 3 but then on the (if (null? l) count line instead of
>> returning out of the function it goes onto the next line (l-iter (cdr l) (+
>> count l)))) and of course fails at cder of an empty list.
>>
>> Racket error message is:
>>
>>  mcdr: contract violation
>>   expected: mpair?
>>   given: '()
>>
>>
>> I am fairly new to scheme.  What am I doing wrong?
>>
>> Angus
>>
>>
>>
>> ---------- Forwarded message ----------
>> From: Pierpaolo Bernardi <olopierpa at gmail.com>
>> To: Bo Gus <forumangus at gmail.com>
>> Cc: Racket Newsgroup <users at racket-lang.org>
>> Date: Wed, 20 Nov 2013 12:27:40 +0100
>> Subject: Re: [racket] Why does this tail recursive list length function
>> fail at cdr l at end of list?
>> On Wed, Nov 20, 2013 at 12:09 PM, Bo Gus <forumangus at gmail.com> wrote:
>> > My tail recursive implementation of length is like this:
>> >
>> > (define (length2 l)
>> >   (define (l-iter l count)
>> >     (if (null? 1) count
>> >         (l-iter (cdr l) (+ count 1))))
>> >   (l-iter l 0))
>>
>> > I am fairly new to scheme.  What am I doing wrong?
>>
>> You wrote (null? 1), which is always false.  Note 1 instead of l.
>>
>> Cheers
>>
>>
>>
>> ---------- Forwarded message ----------
>> From: Bo Gus <forumangus at gmail.com>
>> To: Racket Newsgroup <users at racket-lang.org>
>> Cc:
>> Date: Wed, 20 Nov 2013 11:33:18 +0000
>> Subject: Re: [racket] Why does this tail recursive list length function
>> fail at cdr l at end of list?
>> I noticed that just after posting.
>>
>> I changed to:
>> (define (length2 lst)
>>   (define (l-iter l count)
>>     (if (null? l) count
>>         (l-iter (cdr l) (+ count 1))))
>>   (l-iter lst 0))
>>
>> Must get better at spotting obvious mistakes...
>>
>>
>> On 20 November 2013 11:27, Pierpaolo Bernardi <olopierpa at gmail.com>wrote:
>>
>>> On Wed, Nov 20, 2013 at 12:09 PM, Bo Gus <forumangus at gmail.com> wrote:
>>> > My tail recursive implementation of length is like this:
>>> >
>>> > (define (length2 l)
>>> >   (define (l-iter l count)
>>> >     (if (null? 1) count
>>> >         (l-iter (cdr l) (+ count 1))))
>>> >   (l-iter l 0))
>>>
>>> > I am fairly new to scheme.  What am I doing wrong?
>>>
>>> You wrote (null? 1), which is always false.  Note 1 instead of l.
>>>
>>> Cheers
>>>
>>
>>
>>
>> ---------- Forwarded message ----------
>> From: Daniel Prager <daniel.a.prager at gmail.com>
>> To: Bo Gus <forumangus at gmail.com>, Racket Users <users at racket-lang.org>
>> Cc:
>> Date: Wed, 20 Nov 2013 22:52:17 +1100
>> Subject: Re: [racket] Why does this tail recursive list length function
>> fail at cdr l at end of list?
>> Replace the expression (null? 1) with (null? l). You appear to have typed
>> 1 (one) where you meant l (ell).
>> As a general rule-of-thumb a name such as lst or a-list will reduce the
>> risk of this kind of error.
>>
>> Dan
>>
>>
>> ---------- Forwarded message ----------
>> From: Daniel Prager <daniel.a.prager at gmail.com>
>> To: Bo Gus <forumangus at gmail.com>, Racket Users <users at racket-lang.org>
>> Cc:
>> Date: Wed, 20 Nov 2013 22:56:25 +1100
>> Subject: Re: [racket] Why does this tail recursive list length function
>> fail at cdr l at end of list?
>> Oops - dup.
>>
>> Didn't see Pierpaolo's reply.
>>
>> Dan
>>
>>
>> On Wed, Nov 20, 2013 at 10:52 PM, Daniel Prager <
>> daniel.a.prager at gmail.com> wrote:
>>
>>> Replace the expression (null? 1) with (null? l). You appear to have
>>> typed 1 (one) where you meant l (ell).
>>> As a general rule-of-thumb a name such as lst or a-list will reduce the
>>> risk of this kind of error.
>>>
>>> Dan
>>>
>>
>>
>>
>> --
>> *Daniel Prager*
>> Agile/Lean Coaching, Software Development and Leadership
>> Twitter: @agilejitsu <https://twitter.com/agilejitsu>
>> Blog: agile-jitsu.blogspot.com
>>
>>
>> ---------- Forwarded message ----------
>> From: Robby Findler <robby at eecs.northwestern.edu>
>> To: Emina Torlak <emina at eecs.berkeley.edu>
>> Cc: users <users at racket-lang.org>, Sam Tobin-Hochstadt <
>> samth at cs.indiana.edu>
>> Date: Wed, 20 Nov 2013 07:19:35 -0600
>> Subject: Re: [racket] obtaining the location of an identifier
>> I didn't try it, but you it might work to use local-expand and then find
>> all the binding forms and then rewrite them to use boxes (or some other
>> source of uniqueness you might have around).
>>
>> Robby
>>
>>
>> On Wed, Nov 20, 2013 at 12:53 AM, Emina Torlak <emina at eecs.berkeley.edu>wrote:
>>
>>> This is how my solution currently works, but unfortunately, it's not
>>> quite right.  Here is a small example that demonstrates why, assuming the
>>> implementation based on free-id-table:
>>>
>>> (define (cell init)
>>>   (let ([x init])
>>>     (case-lambda
>>>       [() x]
>>>       [(v) (set! x v)])))
>>>
>>> (define foo (cell 0))
>>> (define bar (cell 1))
>>>
>>> > (foo 2)
>>> > (bar 3)
>>> > (foo)
>>> 2
>>> > (bar)
>>> 3
>>> > (dict->list global)
>>> '((.#<syntax:18:17 x> . 3))
>>>
>>> In the above scenario, I need the global map to contain two bindings:
>>>  one for the location of 'foo.x' and the other for the location of 'bar.x.'
>>>
>>>
>>> Emina
>>>
>>>
>>>
>>> On Tue, Nov 19, 2013 at 10:31 PM, Sam Tobin-Hochstadt <
>>> samth at cs.indiana.edu> wrote:
>>>
>>>> I think that just identifiers and `free-id-table`s should work here.
>>>> Here's your example:
>>>>
>>>> #lang racket
>>>>
>>>> (require syntax/id-table (only-in racket [set! #%set!]))
>>>>
>>>> (define global (make-free-id-table))
>>>>
>>>> (define-syntax-rule (location-of id) #'id)
>>>>
>>>> (define-syntax-rule (set! id expr)
>>>>   (let ([v expr])
>>>>     (dict-set! global (location-of id) v)
>>>>     (#%set! id v)))
>>>>
>>>> > (define x 1)
>>>> > (set! x 2)
>>>> > (set! x 3)
>>>> > (for/list ([(k v) (in-dict global)]) (list k v))
>>>> '((.#<syntax:4:8 x> 3))
>>>>
>>>> Also at https://gist.github.com/samth/7558673
>>>>
>>>> Sam
>>>>
>>>> On Mon, Nov 18, 2013 at 2:43 PM, Emina Torlak <emina at eecs.berkeley.edu>
>>>> wrote:
>>>> > I'm using Racket to implement a language for which I need to track
>>>> state
>>>> > updates---in particular, variable mutation using set!.  For example,
>>>> > consider this module definition:
>>>> >
>>>> > #lang racket
>>>> >
>>>> > (require (only-in racket [set! #%set!]))
>>>> >
>>>> > (define global (make-hash))
>>>> >
>>>> > (define-syntax-rule (location-of id)
>>>> >   (#%variable-reference id)) ; doesn't quite do the right thing
>>>> >
>>>> > (define-syntax-rule (set! id expr)
>>>> >   (let ([val expr])
>>>> >     (hash-set! global (location-of id) val)
>>>> >     (#%set! id val)))
>>>> >
>>>> > When I evaluate the following sequence of forms against the above
>>>> > definition, I would like the global hash map to contain just one
>>>> binding
>>>> > that maps the location for 'x' to the value 2.  With the above
>>>> > implementation I get two map entries, since variable-reference
>>>> doesn't quite
>>>> > do what I hoped it did:
>>>> >
>>>> >> (define x 0)
>>>> >> (set! x 1)
>>>> >> (set! x 2)
>>>> >> x
>>>> > 2
>>>> >> global
>>>> > '#hash((#<variable-reference> . 1) (#<variable-reference> . 2))
>>>> >
>>>> > Is there another construct in Racket that I could use for this
>>>> purpose?  If
>>>> > not, can something like this be implemented and how much work would it
>>>> > entail?
>>>> >
>>>> > I have a purely macro-based solution that works for the most part,
>>>> but it's
>>>> > fragile and there are corner cases for which it is just wrong.  So,
>>>> before
>>>> > trying to fix that, I was wondering if there is a nicer way to solve
>>>> it by
>>>> > somehow getting handles for variable locations that are comparable
>>>> using eq?
>>>> > or equal?
>>>> >
>>>> > Thanks!
>>>> >
>>>> > Emina
>>>> >
>>>> >
>>>> > ____________________
>>>> >   Racket Users list:
>>>> >   http://lists.racket-lang.org/users
>>>> >
>>>>
>>>
>>>
>>> ____________________
>>>   Racket Users list:
>>>   http://lists.racket-lang.org/users
>>>
>>>
>>
>>
>
> ____________________
>   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/20131120/8fe51870/attachment-0001.html>

Posted on the users mailing list.