What's the best way to do these syntax transforms?

40 views
Skip to first unread message

Christopher Lemmer Webber

unread,
Nov 8, 2019, 9:51:56 AM11/8/19
to Racket Users
I have a need to do two things in a #lang:

- Most importantly, make all strings that appear in the source code
immutable
- Second and not as urgent, I'd like to add a "dot" notation, so that
(foo.bar 1 2 3) expands into (foo 'bar 1 2 3)

It seems to me that both of these needs are similar. I can imagine how
to do both by thinking of the syntax tree like a list structure and
rewrite via recursive descent. I guess I would re-append the src
locations to the new structure. This seems doable.

But is it the best way? I'm guessing maybe there's a more racket'y way
but I'm unsure.

Jay McCarthy

unread,
Nov 8, 2019, 9:56:02 AM11/8/19
to Christopher Lemmer Webber, Racket Users
On Fri, Nov 8, 2019 at 9:51 AM Christopher Lemmer Webber <cwe...@dustycloud.org> wrote:
I have a need to do two things in a #lang:

 - Most importantly, make all strings that appear in the source code
   immutable

Make #%datum turn literal strings `s` into `(string->immutable-string s)`
 
 - Second and not as urgent, I'd like to add a "dot" notation, so that
   (foo.bar 1 2 3) expands into (foo 'bar 1 2 3)

Turn on `read-cdot` and then make the `#%dot` macro put a quote on the RHS and have #%app notice a #%dot and unwrap it

Jay
 
It seems to me that both of these needs are similar.  I can imagine how
to do both by thinking of the syntax tree like a list structure and
rewrite via recursive descent.  I guess I would re-append the src
locations to the new structure.  This seems doable.

But is it the best way?  I'm guessing maybe there's a more racket'y way
but I'm unsure.


--
Jay McCarthy
Associate Professor @ CS @ UMass Lowell
http://jeapostrophe.github.io
Vincit qui se vincit. 

Philip McGrath

unread,
Nov 8, 2019, 11:29:00 AM11/8/19
to Jay McCarthy, Christopher Lemmer Webber, Racket Users
On Fri, Nov 8, 2019 at 9:56 AM Jay McCarthy <jay.mc...@gmail.com> wrote:
On Fri, Nov 8, 2019 at 9:51 AM Christopher Lemmer Webber <cwe...@dustycloud.org> wrote:
I have a need to do two things in a #lang:

 - Most importantly, make all strings that appear in the source code
   immutable

Make #%datum turn literal strings `s` into `(string->immutable-string s)`

 But the default `#%datum` (which expands to `quote`) already does this:
> (immutable? "foo")
#t
Or, for another way of thinking about it, strings that go into syntax objects are interned. The upshot is that literals are immutable by default: a language that wanted mutable literals would have to make special effort via `#%datum`.

-Philip

Matthew Flatt

unread,
Nov 8, 2019, 2:59:00 PM11/8/19
to Philip McGrath, Jay McCarthy, Christopher Lemmer Webber, Racket Users
At Fri, 8 Nov 2019 11:28:46 -0500, Philip McGrath wrote:
> On Fri, Nov 8, 2019 at 9:56 AM Jay McCarthy <jay.mc...@gmail.com> wrote:
>
> > On Fri, Nov 8, 2019 at 9:51 AM Christopher Lemmer Webber <
> > cwe...@dustycloud.org> wrote:
> >
> >> I have a need to do two things in a #lang:
> >>
> >> - Most importantly, make all strings that appear in the source code
> >> immutable
> >>
> >
> > Make #%datum turn literal strings `s` into `(string->immutable-string s)`
> >
>
> But the default `#%datum` (which expands to `quote`) already does this:
> > (immutable? "foo")
> #t
> Or, for another way of thinking about it, strings that go into syntax
> objects are interned.

More precisely, the reader (via `read-syntax`) creates immutable
strings.

If a macro constructs a mutable string and converts it to a syntax
object, the string is not converted to an immutable string. Maybe it
should be.

Christopher Lemmer Webber

unread,
Nov 8, 2019, 5:06:32 PM11/8/19
to Philip McGrath, Jay McCarthy, Racket Users
Huh... somehow I had thought that I had heard that Racket has mutable
strings by default. It cropped up on my TODO list because of that. I
wonder what gave me that impression?

Christopher Lemmer Webber

unread,
Nov 8, 2019, 5:07:45 PM11/8/19
to Jay McCarthy, Racket Users
Jay McCarthy writes:

> On Fri, Nov 8, 2019 at 9:51 AM Christopher Lemmer Webber <
> cwe...@dustycloud.org> wrote:
>
>> I have a need to do two things in a #lang:
>>
>> - Most importantly, make all strings that appear in the source code
>> immutable
>>
>
> Make #%datum turn literal strings `s` into `(string->immutable-string s)`

Philip has seemed to suggest that maybe this isn't necessary (?)
but it's useful to understand how #%datum helps here

>> - Second and not as urgent, I'd like to add a "dot" notation, so that
>> (foo.bar 1 2 3) expands into (foo 'bar 1 2 3)
>>
>
> Turn on `read-cdot` and then make the `#%dot` macro put a quote on the RHS
> and have #%app notice a #%dot and unwrap it
>
> Jay

Okay thanks, I'll give that a try!

Philip McGrath

unread,
Nov 8, 2019, 6:59:48 PM11/8/19
to Matthew Flatt, Jay McCarthy, Christopher Lemmer Webber, Racket Users
On Fri, Nov 8, 2019 at 2:58 PM Matthew Flatt <mfl...@cs.utah.edu> wrote:
More precisely, the reader (via `read-syntax`) creates immutable
strings.

If a macro constructs a mutable string and converts it to a syntax
object, the string is not converted to an immutable string. Maybe it
should be.

I see now that this is true, but it isn't what I'd expected. The docs for `datum->syntax` say that:
For any kind of value other than a pair, vector, box, immutable hash table, immutable prefab structure, or syntax object, conversion means wrapping the value with lexical information, source-location information, and properties after the value is interned via datum-intern-literal.
and `datum-intern-literal` says that "mutable strings and byte strings are interned as immutable strings and byte strings," which I just confirmed is true.

Based on this, I'd previously thought that using `datum->syntax` on a mutable string would convert the string with `datum-intern-literal`, which would make the wrapped string immutable—but I see now that the wrapped string is, in fact, still mutable.

-Philip

Philip McGrath

unread,
Nov 8, 2019, 7:11:45 PM11/8/19
to Christopher Lemmer Webber, Jay McCarthy, Racket Users
On Fri, Nov 8, 2019 at 5:06 PM Christopher Lemmer Webber <cwe...@dustycloud.org> wrote:
Huh... somehow I had thought that I had heard that Racket has mutable
strings by default.  It cropped up on my TODO list because of that.  I
wonder what gave me that impression?

Racket strings are annoyingly mutable, enough so that there are many reasonable definitions of "default" for which the statement "Racket has mutable strings by default" would be true. For example:
> (immutable? (string-append ""))
#f
String literals are an exception to the general rule, though there is some precedent for that exception: IIRC mutating a string literal in C is undefined behavior.

This is a bit of a pet peeve of mine: https://github.com/racket/rhombus-brainstorming/issues/22

-Philip
Reply all
Reply to author
Forward
0 new messages