RE: [racket-users] Questions: free-identifier=?; literal-id in syntax-case

2015-04-05 Thread Jos Koot
Hi Alexander D. Knauth
 
Thanks, for your very clear answers.
(require (rename-in racket (+ plus))) works too.
A silly mistake of mine to think define would do the same. It does not, of
course.
 
Your argument that a literal-id may occur more than once in a pattern makes
sense.
I'll play with syntax-parse and (_ (~and + (~literal +)) (~literal +)).
May be I am able to make a syntax transformer, say my-syntax-case,
available in expansion-phase,
that does what you have suggested.
 
Thanks again,
Jos Koot
 

  _  

From: racket-users@googlegroups.com [mailto:racket-users@googlegroups.com]
On Behalf Of Alexander D. Knauth
Sent: sábado, 04 de abril de 2015 19:35
To: Jos Koot
Cc: racket-users@googlegroups.com
Subject: Re: [racket-users] Questions: free-identifier=?; literal-id in
syntax-case



On Apr 4, 2015, at 11:22 AM, Jos Koot jos.k...@gmail.com wrote:


The following puzzles me:
 
#lang racket
(define plus +)
(free-identifier=? #'+ #'plus) ; - #f
 
#lang racket
(define-syntax (a stx)
 (syntax-case stx ()
  ((_) (datum-syntax stx (free-identifier=? #'+ #'plus)
(define plus +)
(a) ; - #f
 
#lang racket
(define plus +)
(define-syntax (a stx)
 (syntax-case stx (+)
  ((_ +) #'#t)
  ((_ x) #'#f)))
(a plus) ; - #f
 
I am confused, because I expect #t to be produced in the three above cases.
Obviously I don't understand free-identifier=? well.
Can you help me with that?


Maybe you’re thinking of what happens when you use
(define-syntax plus (make-rename-transformer #’+))
Instead of
(define plus +)


As another question: the docs on syntax-case state:
 
An id that has the same binding as a literal-id matches a syntax object
that is an identifier with the same binding in the sense of
free-identifier=?
file:///C:/Program%20Files/Racket/doc/reference/stxcmp.html?q=syntax-case#%
28def._%28%28quote._~23~25kernel%29._free-identifier~3d~3f%29%29 .
The match does not introduce any
file:///C:/Program%20Files/Racket/doc/reference/stx-patterns.html?q=syntax-
case#%28tech._pattern._variable%29 pattern variables.
 
Why isn't or cant't the match introduce a pattern variable?
Without binding the literal-id as a pattern variable,
location information is lost, for example:
 
#lang racket
(error-print-source-location #t)
(begin-for-syntax (error-print-source-location #t))
(define-syntax (a stx)
 (syntax-case stx (+)
  ((_ +) (raise-syntax-error 'a msg stx #'+ ; ---
(a +)
 
raises a syntax-error as expected, but it highlights + in the line marked
---,
not in the last line.
I would prefer the + in the last line to be highlighted.


I mostly agree with that idea, but there is a reason why it doesn’t do that.
If it did that, then I don’t think you could do things like put multiple of
these in a single pattern, so for instance:
(define-syntax (a stx)
  (syntax-case stx (+)
[(_ + +) (raise-syntax-error ‘a “msg” stx #’+)]))
(a +)
It wouldn’t know which + to bind.

The way I do this which feels a bit clunky like there should be a better
solution:
(define-syntax (a stx)
  (syntax-parse stx
[(_ (~and + (~literal +)) (~literal +)) (raise-syntax-error ‘a “msg” stx
#’+)]))
To tell it specifically to bind it.

To make it a little less verbose, you could define a pattern-expander that
expanded to (~and + (~literal +)), but I’m not sure how to do better than
that.



-- 
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.

-- 
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] Questions: free-identifier=?; literal-id in syntax-case

2015-04-05 Thread Jos Koot
I never used syntax-parse before. I have to look into it.
Many thanks, of course,
Jos Koot

  _  

From: racket-users@googlegroups.com [mailto:racket-users@googlegroups.com]
On Behalf Of Alexander D. Knauth
Sent: domingo, 05 de abril de 2015 15:08
To: Jos Koot
Cc: racket-users@googlegroups.com
Subject: Re: [racket-users] Questions: free-identifier=?; literal-id in
syntax-case



On Apr 5, 2015, at 4:07 AM, Jos Koot jos.k...@gmail.com wrote:


Your argument that a literal-id may occur more than once in a pattern makes
sense.
I'll play with syntax-parse and (_ (~and + (~literal +)) (~literal +)).
May be I am able to make a syntax transformer, say my-syntax-case,
available in expansion-phase,
that does what you have suggested.
 
Thanks again,
Jos Koot


You could also use this, from Ryan Culpepper's reply on another thread:
#lang racket
(require (for-syntax syntax/parse))
(define-syntax (a stx)
 (syntax-parse stx #:literals (+)
  [(_ op:+) (raise-syntax-error 'a msg stx #'op)]))
(a +)



  _  

On Apr 4, 2015, at 11:22 AM, Jos Koot jos.k...@gmail.com wrote:


As another question: the docs on syntax-case state:

 
An id that has the same binding as a literal-id matches a syntax object
that is an identifier with the same binding in the sense of
free-identifier=?
file:///C:/Program%20Files/Racket/doc/reference/stxcmp.html?q=syntax-case#%
28def._%28%28quote._~23~25kernel%29._free-identifier~3d~3f%29%29 .
The match does not introduce any
file:///C:/Program%20Files/Racket/doc/reference/stx-patterns.html?q=syntax-
case#%28tech._pattern._variable%29 pattern variables.
 
Why isn't or cant't the match introduce a pattern variable?
Without binding the literal-id as a pattern variable,
location information is lost, for example:
 
#lang racket
(error-print-source-location #t)
(begin-for-syntax (error-print-source-location #t))
(define-syntax (a stx)
 (syntax-case stx (+)
  ((_ +) (raise-syntax-error 'a msg stx #'+ ; ---
(a +)
 
raises a syntax-error as expected, but it highlights + in the line marked
---,
not in the last line.
I would prefer the + in the last line to be highlighted.


I mostly agree with that idea, but there is a reason why it doesn't do that.
If it did that, then I don't think you could do things like put multiple of
these in a single pattern, so for instance:
(define-syntax (a stx)
  (syntax-case stx (+)
[(_ + +) (raise-syntax-error 'a msg stx #'+)]))
(a +)
It wouldn't know which + to bind.

The way I do this which feels a bit clunky like there should be a better
solution:
(define-syntax (a stx)
  (syntax-parse stx
[(_ (~and + (~literal +)) (~literal +)) (raise-syntax-error 'a msg stx
#'+)]))
To tell it specifically to bind it.

To make it a little less verbose, you could define a pattern-expander that
expanded to (~and + (~literal +)), but I'm not sure how to do better than
that.


-- 
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.

-- 
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] Questions: free-identifier=?; literal-id in syntax-case

2015-04-04 Thread Jos Koot
The following puzzles me:
 
#lang racket
(define plus +)
(free-identifier=? #'+ #'plus) ; - #f
 
#lang racket
(define-syntax (a stx)
 (syntax-case stx ()
  ((_) (datum-syntax stx (free-identifier=? #'+ #'plus)
(define plus +)
(a) ; - #f
 
#lang racket
(define plus +)
(define-syntax (a stx)
 (syntax-case stx (+)
  ((_ +) #'#t)
  ((_ x) #'#f)))
(a plus) ; - #f
 
I am confused, because I expect #t to be produced in the three above cases.
Obviously I don't understand free-identifier=? well.
Can you help me with that?
 
As another question: the docs on syntax-case state:
 
An id that has the same binding as a literal-id matches a syntax object
that is an identifier with the same binding in the sense of
free-identifier=?
file:///C:/Program%20Files/Racket/doc/reference/stxcmp.html?q=syntax-case#%
28def._%28%28quote._~23~25kernel%29._free-identifier~3d~3f%29%29 .
The match does not introduce any
file:///C:/Program%20Files/Racket/doc/reference/stx-patterns.html?q=syntax-
case#%28tech._pattern._variable%29 pattern variables.
 
Why isn't or cant't the match introduce a pattern variable?
Without binding the literal-id as a pattern variable,
location information is lost, for example:
 
#lang racket
(error-print-source-location #t)
(begin-for-syntax (error-print-source-location #t))
(define-syntax (a stx)
 (syntax-case stx (+)
  ((_ +) (raise-syntax-error 'a msg stx #'+ ; ---
(a +)
 
raises a syntax-error as expected, but it highlights + in the line marked
---,
not in the last line.
I would prefer the + in the last line to be highlighted.
 
(I am running DrRacket, version 6.1.1 [3m])
 
Help very much appreciated.
 
Wish all of you a fine easter,
Jos Koot
 

-- 
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] Questions: free-identifier=?; literal-id in syntax-case

2015-04-04 Thread Alexander D. Knauth

On Apr 4, 2015, at 11:22 AM, Jos Koot jos.k...@gmail.com wrote:

 The following puzzles me:
  
 #lang racket
 (define plus +)
 (free-identifier=? #'+ #'plus) ; - #f
  
 #lang racket
 (define-syntax (a stx)
  (syntax-case stx ()
   ((_) (datum-syntax stx (free-identifier=? #'+ #'plus)
 (define plus +)
 (a) ; - #f
  
 #lang racket
 (define plus +)
 (define-syntax (a stx)
  (syntax-case stx (+)
   ((_ +) #'#t)
   ((_ x) #'#f)))
 (a plus) ; - #f
  
 I am confused, because I expect #t to be produced in the three above cases.
 Obviously I don't understand free-identifier=? well.
 Can you help me with that?

Maybe you’re thinking of what happens when you use
(define-syntax plus (make-rename-transformer #’+))
Instead of
(define plus +)

 As another question: the docs on syntax-case state:
  
 An id that has the same binding as a literal-id matches a syntax object
 that is an identifier with the same binding in the sense of free-identifier=?.
 The match does not introduce any pattern variables.
  
 Why isn't or cant't the match introduce a pattern variable?
 Without binding the literal-id as a pattern variable,
 location information is lost, for example:
  
 #lang racket
 (error-print-source-location #t)
 (begin-for-syntax (error-print-source-location #t))
 (define-syntax (a stx)
  (syntax-case stx (+)
   ((_ +) (raise-syntax-error 'a msg stx #'+ ; ---
 (a +)
  
 raises a syntax-error as expected, but it highlights + in the line marked 
 ---,
 not in the last line.
 I would prefer the + in the last line to be highlighted.

I mostly agree with that idea, but there is a reason why it doesn’t do that.
If it did that, then I don’t think you could do things like put multiple of 
these in a single pattern, so for instance:
(define-syntax (a stx)
  (syntax-case stx (+)
[(_ + +) (raise-syntax-error ‘a “msg” stx #’+)]))
(a +)
It wouldn’t know which + to bind.

The way I do this which feels a bit clunky like there should be a better 
solution:
(define-syntax (a stx)
  (syntax-parse stx
[(_ (~and + (~literal +)) (~literal +)) (raise-syntax-error ‘a “msg” stx 
#’+)]))
To tell it specifically to bind it.

To make it a little less verbose, you could define a pattern-expander that 
expanded to (~and + (~literal +)), but I’m not sure how to do better than that.


-- 
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.