<div dir="ltr">On Thu, May 30, 2013 at 12:25 PM, Eric Dobson <span dir="ltr">&lt;<a href="mailto:eric.n.dobson@gmail.com" target="_blank">eric.n.dobson@gmail.com</a>&gt;</span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Why do literal-sets have to be unhygienic? I expected them to work<br>
like literal lists which you say are hygienic. Also literal-sets are<br>
not documented as being unhygienic, which was the confusing part.<br></blockquote><div><br></div><div>Literal sets are hygienic in the informal sense of &quot;follow a disciplined notion of binding structure&quot;.  They are not hygienic in the formal sense of Scheme and Racket expansion wherein names derive their scope from where they appear in the source.  Specifically, when you use a literal set, you do not write down the names in the set, yet they are scoped as if you wrote them right there in your program.  Which is what you want -- to use those names in the current scope -- but is not hygienic, by the strict definition.  Anything where you &quot;store&quot; names and pull them out later -- module exports, unit signatures, literal sets, and other similar tools -- is necessarily unhygienic.  Using them in surface syntax is relatively straightforward; using them in macros is tricky, because these tools have to introduce the stored names into some lexical context for you to use, and with a macro that could be either the macro&#39;s context or the macro user&#39;s context.<br>

</div><div> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
In my example 2 both the literals in the literal-list and the ids used<br>
to match the syntax have the same marks, so it is very unintuitive<br>
that the lexical context of the literal-set name should matter. Also<br>
can literal sets be used to abstract over identifiers with different<br>
lexical contexts, intuitively since other forms can do that I would<br>
expect that they should be able to, but given that its unhygienic I&#39;m<br>
not so sure now.<br></blockquote><div><br></div><div>Because literal-sets can be used in any context, the names used in the literal set are used in the context of the reference to the literal set.  So if a macro injects the name of the literal set to a syntax-parse expression, the macro needs to determine whether that literal set is used in the macro&#39;s context or the macro user&#39;s context.  Otherwise it could be difficult to use the names in the macro itself.  Hence the #:at option, which lets macros specify the context &quot;at&quot; which the literal set is used, if it&#39;s different from the context naming the literal set.<br>

</div><div><br></div><div>The short, short version: use the #:at option if your macro uses literal sets &quot;on behalf of&quot; the user.<br><br></div><div>The syntax-parse documentation could probably be more explicit about how literals in literal sets derive their context; I do not envy Ryan the task of writing that up, because I&#39;m not sure how to make it 100% clear myself, as my ramblings above may show.<br>

</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Thu, May 30, 2013 at 3:49 AM, Ryan Culpepper &lt;<a href="mailto:ryanc@ccs.neu.edu">ryanc@ccs.neu.edu</a>&gt; wrote:<br>

<div class="HOEnZb"><div class="h5">
&gt; On 05/29/2013 03:30 AM, Eric Dobson wrote:<br>
&gt;&gt;<br>
&gt;&gt; I was writing a macro that generated a literal-set and ran into some<br>
&gt;&gt; confusing behavior which I have distilled to the following program.<br>
&gt;&gt;<br>
&gt;&gt; #lang racket<br>
&gt;&gt; (require syntax/parse<br>
&gt;&gt;           (for-syntax syntax/parse))<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; (define-syntax (define-ls1 stx)<br>
&gt;&gt;    (syntax-parse stx<br>
&gt;&gt;      ((_ name:id (lit:id ...))<br>
&gt;&gt;       #&#39;(define-literal-set name (lit ...)))))<br>
&gt;&gt;<br>
&gt;&gt; (define-ls1 ls1 (+))<br>
&gt;&gt; (define-syntax-class sc1<br>
&gt;&gt;    #:literal-sets (ls1)<br>
&gt;&gt;    (pattern +))<br>
&gt;&gt;<br>
&gt;&gt; (for/list ((x (list #&#39;+ #&#39;*)))<br>
&gt;&gt;    (syntax-parse x<br>
&gt;&gt;      (x:sc1 #t)<br>
&gt;&gt;      (_ #f)))<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; (define-syntax (define-sc2 stx)<br>
&gt;&gt;    (syntax-parse stx<br>
&gt;&gt;      ((_ name:id (lit:id ...))<br>
&gt;&gt;       #&#39;(begin<br>
&gt;&gt;           (define-literal-set inner (lit ...))<br>
&gt;&gt;           (define-syntax-class name<br>
&gt;&gt;             #:literal-sets (inner)<br>
&gt;&gt;             (pattern lit) ...)))))<br>
&gt;&gt;<br>
&gt;&gt; (define-sc2 sc2 (+))<br>
&gt;&gt; (for/list ((x (list #&#39;+ #&#39;*)))<br>
&gt;&gt;    (syntax-parse x<br>
&gt;&gt;      (x:sc2 #t)<br>
&gt;&gt;      (_ #f)))<br>
&gt;&gt;<br>
&gt;&gt; (define-syntax (define-sc3 stx)<br>
&gt;&gt;    (syntax-parse stx<br>
&gt;&gt;      ((_ name:id inner:id (lit:id ...))<br>
&gt;&gt;       #&#39;(begin<br>
&gt;&gt;           (define-literal-set inner (lit ...))<br>
&gt;&gt;           (define-syntax-class name<br>
&gt;&gt;             #:literal-sets (inner)<br>
&gt;&gt;             (pattern lit) ...)))))<br>
&gt;&gt;<br>
&gt;&gt; (define-sc3 sc3 inner3 (+))<br>
&gt;&gt; (for/list ((x (list #&#39;+ #&#39;*)))<br>
&gt;&gt;    (syntax-parse x<br>
&gt;&gt;      (x:sc3 #t)<br>
&gt;&gt;      (_ #f)))<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; (define-syntax (define-sc4 stx)<br>
&gt;&gt;    (syntax-parse stx<br>
&gt;&gt;      ((_ name:id (lit:id ...))<br>
&gt;&gt;       #&#39;(begin<br>
&gt;&gt;           (define-literal-set inner (lit ...))<br>
&gt;&gt;           (define-syntax-class name<br>
&gt;&gt;             #:literal-sets ((inner #:at name))<br>
&gt;&gt;             (pattern lit) ...)))))<br>
&gt;&gt;<br>
&gt;&gt; (define-sc4 sc4 (+))<br>
&gt;&gt; (for/list ((x (list #&#39;+ #&#39;*)))<br>
&gt;&gt;    (syntax-parse x<br>
&gt;&gt;      (x:sc4 #t)<br>
&gt;&gt;      (_ #f)))<br>
&gt;&gt;<br>
&gt;&gt; This produces the output:<br>
&gt;&gt; &#39;(#t #f)<br>
&gt;&gt; &#39;(#t #t)<br>
&gt;&gt; &#39;(#t #f)<br>
&gt;&gt; &#39;(#t #f)<br>
&gt;&gt;<br>
&gt;&gt; I would have expected the second one to return &#39;(#t #f) like the first<br>
&gt;&gt; but it doesn&#39;t.<br>
&gt;<br>
&gt;<br>
&gt; The issue is how syntax-parse decides whether an identifier in a pattern is<br>
&gt; a pattern variable or a literal. Let&#39;s take the simple case, where we have<br>
&gt; just a literals list. From the standpoint of hygiene, the literals list is<br>
&gt; &quot;binding-like&quot; because it sets the interpretation for &quot;references&quot; in the<br>
&gt; patterns. That means the relevant comparison is bound-identifier=? (like all<br>
&gt; binding forms), and that means that at a minimum if you want an identifier<br>
&gt; in a pattern to be considered a literal, it must have the same marks as the<br>
&gt; corresponding identifier in the literals list. Consider the following<br>
&gt; program:<br>
&gt;<br>
&gt; (define-syntax-rule<br>
&gt;   (define-syntax-rule/arith (macro . pattern) template)<br>
&gt;   (define-syntax macro<br>
&gt;     (syntax-rules (+ - * /)<br>
&gt;       [(macro . pattern) template])))<br>
&gt;<br>
&gt; (define-syntax-rule/arith (sum-left-arg (+ x y)) x)<br>
&gt;<br>
&gt; One might expect sum-left-arg to raise a syntax error if given a term with<br>
&gt; something other than + in operator position. But in fact:<br>
&gt;<br>
&gt; (sum-left-arg (* 1 2))<br>
&gt; ;; =&gt; 1<br>
&gt;<br>
&gt; The reason is because the expansion of define-syntax-rule/arith puts marks<br>
&gt; on the + in the literals list. So only +&#39;s with the same mark in the pattern<br>
&gt; are considered literals; all others are pattern variables. In particular,<br>
&gt; the + in the pattern in the definition of sum-left-arg is unmarked, so it&#39;s<br>
&gt; a pattern variable.<br>
&gt;<br>
&gt; Now back to literal sets. A #:literal-sets clause is an essentially like an<br>
&gt; unhygienic binding form. The identifiers it &quot;binds&quot; must be given some<br>
&gt; lexical context; it defaults to the lexical context of the literal set name.<br>
&gt; In define-sc2 from your example, that is inner, which is introduced by the<br>
&gt; macro and thus has a mark on it. So it&#39;s just like you had a literals list<br>
&gt; consisting of a marked +. But the + in the pattern is unmarked, since it<br>
&gt; comes from the original program (via the lit pattern variable). They don&#39;t<br>
&gt; match, so the identifier in the pattern is interpreted as a pattern<br>
&gt; variable.<br>
&gt;<br>
&gt;<br>
&gt;&gt; The third and fourth are ways to get it to, but I&#39;m<br>
&gt;&gt;<br>
&gt;&gt; not sure why they work. The third seems to show that the meaning of<br>
&gt;&gt; how a literal set binds variables depends on the context of the<br>
&gt;&gt; literal-set name and not the actual literals.<br>
&gt;<br>
&gt;<br>
&gt; In define-sc3, inner also comes from the original program, so it&#39;s unmarked,<br>
&gt; so the literals consist of an unmarked +, which matches the + in the<br>
&gt; pattern.<br>
&gt;<br>
&gt;<br>
&gt;&gt; The fourth way uses #:at<br>
&gt;&gt;<br>
&gt;&gt; which is documented as being useful to macros, but not much else. Can<br>
&gt;&gt; someone explain how literal-sets are supposed to determine whether or<br>
&gt;&gt; not a variable in the pattern is treated as a literal or as a pattern<br>
&gt;&gt; variable?<br>
&gt;<br>
&gt;<br>
&gt; An #:at clause changes the lexical context given to literals. In define-sc4,<br>
&gt; inner is back to being marked, but that doesn&#39;t matter, because the literals<br>
&gt; are given the lexical context of the syntax bound to name. In the example,<br>
&gt; that&#39;s sc4, which comes from the macro use and is thus unmarked.<br>
&gt;<br>
&gt; All unhygienic binding forms have similar difficulties. Macros that expand<br>
&gt; into require forms are another common source of confusion.<br>
&gt;<br>
&gt; Ryan<br>
&gt;<br>
____________________<br>
  Racket Users list:<br>
  <a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
<br>
</div></div></blockquote></div><br></div></div>