Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R7RS does not support underscore as a keyword in syntax-rules #413

Open
ar-nelson opened this issue Nov 16, 2019 · 7 comments
Open

R7RS does not support underscore as a keyword in syntax-rules #413

ar-nelson opened this issue Nov 16, 2019 · 7 comments

Comments

@ar-nelson
Copy link

In other R7RS schemes, it is possible to define a keyword named _ in syntax-rules, and that keyword will be detected in pattern matching. Gerbil appears to always reserve _ as a wildcard, even if it has been named as a keyword.

For example, this script underscore-keyword.scm:

(import (scheme base)
        (scheme write))

(define-syntax is-underscore
  (syntax-rules (_)
    ((is-underscore _) #t)
    ((is-underscore x) #f)))

(display (list (is-underscore _) (is-underscore 1)))
(newline)

Gauche and Chibi match the first case when passed the literal symbol _, and the second case otherwise; they print (#t #f). But Gerbil treats the _ as a wildcard and always matches the first case; it prints (#t #t).

$ gosh -r7 underscore-keyword.scm
(#t #f)
$ chibi-scheme underscore-keyword.scm
(#t #f)
$ gxi --lang r7rs underscore-keyword.scm
(#t #t)
@vyzo
Copy link
Collaborator

vyzo commented Nov 17, 2019

I don't think that behaviour is specified in the standard though.
_ goes deep in gerbil, it's recognized as the "ignore/wildcard" identifier in many forms.

@vyzo
Copy link
Collaborator

vyzo commented Nov 17, 2019

Also note that there is a predicate bound at phi=1 which you can use to test for underscores: underscore?.

@ar-nelson
Copy link
Author

ar-nelson commented Nov 17, 2019

@vyzo I don't think it is either--actually, when I CTRL-F the R7RS document for _, it looks like it doesn't even describe its usage as a wildcard in syntax-rules patterns, even though every Scheme does that.

But there's good reason to support using _ as a keyword: what if you want to define your own syntax that uses _ as a wildcard? In this case, I had a macro for my own pattern-matching syntax on top of R7RS, and I had to take out the _ wildcard case to make it compile in Gerbil. There's probably a way to make this work using cond-expand and underscore?, but that's just another Gerbil-specific kludge in the codebase.

@vyzo
Copy link
Collaborator

vyzo commented Nov 18, 2019

There is no need for much trickery, you can use a fender in the syntax-rules pattern.
For instance:

(defrules is-underscore? ()
  ((_ x) (underscore? #'x) #t)
  ((_ x) #f)))

@vyzo
Copy link
Collaborator

vyzo commented Nov 18, 2019

Btw, why don't you just use the native match on Gerbil?
It is well tuned and compiler optimized.

@ar-nelson
Copy link
Author

ar-nelson commented Nov 18, 2019

@vyzo Because I'm writing an application in R7RS so that it can be portable to as many platforms as possible, and as many Schemes as possible. I don't want to use any Gerbil-specific features in the portable parts of the code.

I'm not using Gerbil for Gerbil's syntax, I'm using it because it's an R7RS Scheme that compiles to fast native code.

@vyzo
Copy link
Collaborator

vyzo commented Nov 18, 2019

That's totally fine, Gerbi is a meta-language after all.
You could however cond-expand and use the native gerbil match when appropriate, it probably has similar syntax to your match (or could be molded to do so with the macro capabiities), and you'd get faster pattern matching optimized by the compiler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants