[racket] Typesetting with subscripts in Redex

From: Robby Findler (robby at eecs.northwestern.edu)
Date: Wed Sep 12 16:38:40 EDT 2012

On Mon, Sep 10, 2012 at 12:05 AM, Nada Amin <namin at alum.mit.edu> wrote:
> On Mon, Sep 10, 2012 at 6:46 AM, Robby Findler
> <robby at eecs.northwestern.edu> wrote:
>> On Sun, Sep 9, 2012 at 6:52 PM, Matthew Flatt <mflatt at cs.utah.edu> wrote:
>>> Getting the line, column, and span for a rewriter result can be tricky.
>>> Usually, you want the result of a rewriter to have the same start and
>>> span as the input. I think I got that part right, below, too.
>>
>> with-compound-rewriter has some smarts about how to insert lw structs
>> when it sees non-lws in the resulting list. So you can exploit that by
>> writing something like this (and thus avoid the need for "collapse"):
>>
>> (define (dummy-rewriters thunk)
>>   (with-compound-rewriters
>>    (['n (lambda (lws)
>>           (list ""
>>                 (list-ref lws 2)
>>                 (text (symbol->string (lw-e (list-ref lws 1)))
>>                       `(subscript . ,(default-style))
>>                       (default-font-size))
>>                 ""))])
>>    (thunk)))
>>
>> I've added a note about this trick to the docs.
> The behavior is not the same as Matthew Flatt's solution. With:
> (dummy-rewriters (lambda () (render-term dummy (
>   (n 112221111) (n
>   1223333)))))
> I get just one garbled line with this trick, while with Matthew
> Flatt's solution, I get two lines as expected.
> Is this bug in the trick's implementation?

Oh, yes. I believe this intended. When you put that "" in there, it
means, roughly, "eliminate all logical space" including, in this case,
the vertical space. So it tries to put the "1223333" on the same line
as the "n", but in the same column where it appears in the source.

If you use more conventional indentation, something like this:

(dummy-rewriters (lambda () (render-term dummy (
  abcdefgh (n
            1223333)))))

then you'll see the "1223333" next to there abcdefgh, instead of below it.

If you were writing things like this a lot, you could use this rewriter:

(define (dummy-rewriters thunk)
  (with-compound-rewriters
   (['n (lambda (lws)
          (list (list-ref lws 2)
                (text (symbol->string (lw-e (list-ref lws 1)))
                      `(subscript . ,(default-style))
                      (default-font-size))
                ""))])
   (thunk)))


but that is probably not what you want in general, since the logical
space is not going to be absorbed, so that can look strange in other
(probably more common) situations.

Robby

Posted on the users mailing list.