[racket-dev] syntax/syntax proposal
Hi all, Recently I was using the `stx-car` function from `syntax/stx`. At some point, I had called it on a non-syntax pair and the error message came from `car`, which is used inside the implementation of `stx-car`. I thought it would be nice to add contracts in `syntax/stx` for better error messages, but it turns out that the contract library depends on it and so it's impossible to do without introducing cycles. So I would like to propose a `syntax/syntax` library with the following: * provides `syntax/stx` functions with contracts attached (caveat: core libraries like `racket/contract` would still use `syntax/stx`) * for consistency with the rest of the language, `stx-car` and friends would be renamed to use the `syntax-` prefix instead of `stx-`. * the name of the library is also consistent with the rest of the language. This could replace `syntax/stx` for most users and provide both better error messages and identifiers that follow standard Racket naming convention. I've attached a patch that implements this. Any comments? Cheers, Asumu From f480e8511c6f162e6a35d06861fb5dcd5a3bfb7a Mon Sep 17 00:00:00 2001 From: Asumu Takikawa as...@ccs.neu.edu Date: Fri, 15 Jun 2012 15:00:20 -0400 Subject: [PATCH] Add syntax/syntax library (aliases for syntax/stx functions with contracts) --- collects/syntax/syntax.rkt | 24 1 file changed, 24 insertions(+) create mode 100644 collects/syntax/syntax.rkt diff --git a/collects/syntax/syntax.rkt b/collects/syntax/syntax.rkt new file mode 100644 index 000..73261b7 --- /dev/null +++ b/collects/syntax/syntax.rkt @@ -0,0 +1,24 @@ +#lang racket/base + +;; syntax utilities + +(require + racket/contract/base + (rename-in syntax/stx +[stx-null? syntax-null?] +[stx-pair? syntax-pair?] +[stx-list? syntax-list?] +[stx-car syntax-car] +[stx-cdr syntax-cdr] +[stx-list syntax-list] +[stx-map syntax-map])) + +(provide/contract + [syntax-null? (- any/c boolean?)] + [syntax-pair? (- any/c boolean?)] + [syntax-list? (- any/c boolean?)] + [syntax-car (- syntax-pair? any)] + [syntax-cdr (- syntax-pair? any)] + [syntax-list (- syntax-list? (or/c list? #f))] + [syntax-map (-* (procedure?) #:rest syntax-list? any)] + [module-or-top-identifier=? (- identifier? identifier? boolean?)]) \ No newline at end of file -- 1.7.10 _ Racket Developers list: http://lists.racket-lang.org/dev
Re: [racket-dev] syntax/syntax proposal
At Fri, 15 Jun 2012 15:12:05 -0400, Asumu Takikawa wrote: * for consistency with the rest of the language, `stx-car` and friends would be renamed to use the `syntax-` prefix instead of `stx-`. +1 I always get these names wrong. * the name of the library is also consistent with the rest of the language. +1 Vincent _ Racket Developers list: http://lists.racket-lang.org/dev
Re: [racket-dev] syntax/syntax proposal
On 2012-06-15 15:12:05 -0400, Asumu Takikawa wrote: I've attached a patch that implements this. Any comments? Just realized after I sent it that I'd change two things in the patch: * `syntax-null?`, `syntax-pair?`, `syntax-list?` would be defined using `procedure-rename` to get better contract error messages. * the `#:rest` contract is wrong, it'd really be `(listof syntax-list?)` And of course there'd be documentation in the final version too. Cheers, Asumu _ Racket Developers list: http://lists.racket-lang.org/dev
Re: [racket-dev] syntax/syntax proposal
Can we get syntax-first and syntax-rest while you're at it? (I looked for these just a couple of days ago, and like Vincent, I got the stx- wrong) _ Racket Developers list: http://lists.racket-lang.org/dev
Re: [racket-dev] syntax/syntax proposal
At Fri, 15 Jun 2012 15:09:15 -0600, Ryan Culpepper wrote: The 'stx-*' functions work on values that aren't syntax objects, so renaming them to 'syntax-*' would be misleading. Given the name, I would have thought they only worked on syntax objects. Roughly, stx = syntax | null | (cons syntax stx) I had no idea that was the case. The name certainly does not suggest that. The fact that the metavariable for syntax objects is `stx' also does not help. In which cases would I use an `stx' as opposed to a syntax object? I sometimes wonder if we should make a racket/pre-contracts subcollection and just stuff all of racket/contract/base's dependencies in there, then say everything else is allowed (maybe even expected) to use contracts. +1 Vincent _ Racket Developers list: http://lists.racket-lang.org/dev
Re: [racket-dev] syntax/syntax proposal
On Jun 15, 2012, at 5:25 PM, Vincent St-Amour wrote: Roughly, stx = syntax | null | (cons syntax stx) I had no idea that was the case. The name certainly does not suggest that. The fact that the metavariable for syntax objects is `stx' also does not help. In which cases would I use an `stx' as opposed to a syntax object? Sounds like this should be documented and possibly even contracted. I sometimes wonder if we should make a racket/pre-contracts subcollection and just stuff all of racket/contract/base's dependencies in there, then say everything else is allowed (maybe even expected) to use contracts. +1 +2 _ Racket Developers list: http://lists.racket-lang.org/dev
Re: [racket-dev] syntax/syntax proposal
On 2012-06-15 15:09:15 -0600, Ryan Culpepper wrote: The 'stx-*' functions work on values that aren't syntax objects, so renaming them to 'syntax-*' would be misleading. Is that really so misleading though? There is already precedent for functions which take arguments not exactly matching their prefix. `list-ref` works on non-`list?` things, for example. Other examples include `syntax-local-module-exports`, `syntax-local-make-delta-introducer`, `syntax-local-module-required-identifiers`, and so on. 'syntax-list' already exists and means something different from 'stx-list'. This could be `syntax-list-list` instead maybe. Cheers, Asumu _ Racket Developers list: http://lists.racket-lang.org/dev
Re: [racket-dev] syntax/syntax proposal
On 2012-06-15 17:39:27 -0400, Matthias Felleisen wrote: Sounds like this should be documented and possibly even contracted. The contracts I wrote in the patch do reflect this via the `stx-pair?` predicate, FYI. By the way, the definition of a syntax pair in the documentation is this: A syntax pair is a pair containing a syntax object as its first element, and either the empty list, a syntax pair, or a syntax object as its second element. I may just be confused, but doesn't this conflict with the documented behavior of the `stx-pair?` predicate? Returns #t if v is either a pair or a syntax object representing a pair (see syntax pair). Cheers, Asumu _ Racket Developers list: http://lists.racket-lang.org/dev