Re: [racket-users] What are disappeared-uses?

2018-08-17 Thread David Storrs
On Thu, Aug 16, 2018 at 5:28 PM, Alexis King  wrote:

> > On Aug 16, 2018, at 15:25, David Storrs 
> > wrote:
> >
> > I see 'record-disappeared-uses' and 'with-disappeared-uses' in the
> > docs, but there's nothing that makes clear what you would use them
> > for.  Some digging around on the mailing list suggests that they allow
> > Dr Racket to draw an arrow to a relevant source location.  Do they
> > have any use outside of Dr Racket?
>
>
> They’re also utilized by racket-mode (since racket-mode uses the same
> Check Syntax implementation that DrRacket does), as well as a couple
> other tools that statically analyze Racket programs (such as Ryan’s
> check-requires tool). They are only used by tooling, and they do not
> otherwise affect the behavior of Racket programs, but they should be
> used by “well-behaved” macros so that various tools can understand
> things lost in the process of macroexpansion.
> [...]
>

 Thanks, that all makes sense.  I'll make sure to use it in future.

One more question:  Short of actually doing the full expansion and manually
inspecting the results, how do I know what's going to disappear?

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] What are disappeared-uses?

2018-08-16 Thread Alexis King
> On Aug 16, 2018, at 15:25, David Storrs 
> wrote:
> 
> I see 'record-disappeared-uses' and 'with-disappeared-uses' in the
> docs, but there's nothing that makes clear what you would use them
> for.  Some digging around on the mailing list suggests that they allow
> Dr Racket to draw an arrow to a relevant source location.  Do they
> have any use outside of Dr Racket?


They’re also utilized by racket-mode (since racket-mode uses the same
Check Syntax implementation that DrRacket does), as well as a couple
other tools that statically analyze Racket programs (such as Ryan’s
check-requires tool). They are only used by tooling, and they do not
otherwise affect the behavior of Racket programs, but they should be
used by “well-behaved” macros so that various tools can understand
things lost in the process of macroexpansion.

To give an example, consider syntax-parse’s pattern language. When you
write a pattern like

(~optional (~seq #:opt opt-val))

then the identifiers ~optional and ~seq do not actually appear anywhere
in the expansion, since they get compiled by syntax-parse into
pattern-matching forms. However, users would still like to be able to,
say, open the documentation for ~optional in DrRacket or racket-mode. A
naïve implementation of syntax-parse would make this impossible, since
DrRacket and racket-mode inspect the fully-expanded program, and by the
time the program has been expanded, those identifiers are gone.

You can see this for yourself by writing a small macro that uses the
#:literals feature of syntax-parse. For example:

(define-syntax-parser m
  #:literals [=>]
  [(_ x => f)
   #'(f x)])

If you write the expression (m #t => not), then DrRacket or racket-mode
will be able to open the documentation or report the location of the
definition of the not function, but they won’t be able to tell you
anything about the => binding, even though it is documented and is
defined in racket/base. To help Check Syntax out, a “well-behaved” macro
should add the 'disappeared-use syntax property to the expansion for use
by tools, like this:

(define-syntax-parser m
  #:literals [=>]
  [(_ x (~and => arrow) f)
   (syntax-property
#'(f x)
'disappeared-use
(list (syntax-local-introduce #'arrow)))])

Now, tools will be able to detect the use of the => identifier and
behave accordingly.

Doing this bookkeeping is laborious, so I’m happy to report that, when
using syntax-parse, disappeared uses of #:literals can be tracked
automatically as of Racket v7.0. A simpler way to define the
well-behaved version of m would be to use the new #:track-literals
option:

(define-syntax-parser m
  #:literals [=>]
  #:track-literals
  [(_ x => f)
   #'(f x)])

This will work as intended. For all uses of #:literals or ~literal,
syntax-parse will automatically add the necessary 'disappeared-use
property to the output when #:track-literals option is specified.
(Sadly, making this the default could not be done in a completely
backwards-compatible way, so it needs to be opt-in for now.)

In general, when a macro inspects syntax from its input, but throws that
syntax away in its expansion, it should probably include a
'disappeared-use property to communicate this information to tools.
Otherwise, in addition to potentially causing DrRacket and racket-mode
to be less helpful than they could ideally be, it might also cause tools
to mistake identifiers as being completely unused when they actually are
— for example, a require might be marked as unused if it’s only imported
to access a particular literal identifier.

Macros that don’t properly record disappeared-uses are not going to
cause any catastrophic problems, so the bookkeeping might not be worth
the mental overhead to you. It would be nice if the manual annotations
weren’t necessary at all. Still, I think it’s a nice feature, and I find
it annoying when macros I use don’t properly report their disappeared
uses.

Alexis

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] What are disappeared-uses?

2018-08-16 Thread David Storrs
I see 'record-disappeared-uses' and 'with-disappeared-uses' in the docs,
but there's nothing that makes clear what you would use them for.  Some
digging around on the mailing list suggests that they allow Dr Racket to
draw an arrow to a relevant source location.  Do they have any use outside
of Dr Racket?

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.