Guile assert macro
Hello Guile users, I was looking for an assert facility in Guile and found the following: https://www.gnu.org/software/guile/manual/html_node/rnrs-base.html#rnrs-base (Search for assert there to find it on the page.) However, while searching, I also found something that looks even better: http://okmij.org/ftp/Scheme/assert-syntax-rule.txt Is that anywhere implemented in Guile already? (In case it is not implemented: What are the reasons for not providing that one? It looks a little bit more useful than the one I found.) Regards, Zelphir
Re: Guile assert macro
On Mon, Apr 22, 2019 at 08:57:32PM +0200, Zelphir Kaltstahl wrote: > Hello Guile users, > > I was looking for an assert facility in Guile and found the following: > > https://www.gnu.org/software/guile/manual/html_node/rnrs-base.html#rnrs-base > > (Search for assert there to find it on the page.) > > However, while searching, I also found something that looks even better: > > http://okmij.org/ftp/Scheme/assert-syntax-rule.txt This is my assert macro. I'm sure there are dozens of other versions. --- (define-module (mlg assert) #:export (assert assert-type)) (define-syntax __FILE__ (syntax-rules () ((_) (or (assv-ref (current-source-location) 'filename) "(unknown file)" (define-syntax __LINE__ (syntax-rules () ((_) (or (assv-ref (current-source-location) 'line) "(unknown line)" (define-syntax assert (lambda (x) (syntax-case x () ((_ expression) #'(let ((ret expression)) (unless ret (error (format #f "~a:~a: assertion failed: ~a = ~s" (__FILE__) (__LINE__) 'expression expression (define-syntax assert-type (lambda (x) (syntax-case x () [(_ type var) #`(if (not (#,(datum->syntax #'var (string->symbol (string-append (symbol->string (syntax->datum #`type)) "?"))) var)) (scm-error 'wrong-type-arg #f (string-append "not type '" #,(symbol->string (syntax->datum #`type)) "': ~s") (list var) (list var)))])))
Re: Guile assert macro
Hi Mike, Thank you for sharing your assert version. I am more focused on finding out, whether the assert macro fromĀ Oleg Kiselyov's page is already in Guile somewhere (and thus in the standard and usable without installing libs or putting more code unrelated to other stuff inside an example project) and if not, why not. However, I guess your point is, that there are many implementations of some kind of assertion facility? If I see it correctly in the code you wrote, the type test assertion simply puts a question mark at the end of the type argument to the ~assert-type~ macro. I have a few questions about this: Q1: What happens, if there is no such predicate with a question mark at the end? Q2: Why not write the type assertion in terms of basic assert macro you wrote, instead of using another ~syntax-case~? Q3: Does such a type assertion make sense as a separate facility, compared to simply using ~(assert (number? x))~ for example? (Note, that I am not very experienced in macro writing myself. It is still something I need to get an intuition for. In the case of assert, it seems to me, that it is a natural case, where a macro would make sense. Finding Oleg Kiselyov's page going on about the assert macro seems to confirm this with at least an "authority argument".) Regards, Zelphir On 4/22/19 9:36 PM, Mike Gran wrote: > On Mon, Apr 22, 2019 at 08:57:32PM +0200, Zelphir Kaltstahl wrote: >> Hello Guile users, >> >> I was looking for an assert facility in Guile and found the following: >> >> https://www.gnu.org/software/guile/manual/html_node/rnrs-base.html#rnrs-base >> >> (Search for assert there to find it on the page.) >> >> However, while searching, I also found something that looks even better: >> >> http://okmij.org/ftp/Scheme/assert-syntax-rule.txt > This is my assert macro. I'm sure there are dozens of other versions. > > ---
Re: Guile assert macro
Hi Zelphir, Zelphir Kaltstahl writes: > I was looking for an assert facility in Guile and found the following: > > https://www.gnu.org/software/guile/manual/html_node/rnrs-base.html#rnrs-base > > (Search for assert there to find it on the page.) > > However, while searching, I also found something that looks even better: > > http://okmij.org/ftp/Scheme/assert-syntax-rule.txt > > Is that anywhere implemented in Guile already? No. > (In case it is not implemented: What are the reasons for not providing > that one? It looks a little bit more useful than the one I found.) Well, no one has suggested it until now, and moreover I didn't even know it existed :) Do you know if it's seen much use? Do any other Scheme implementations include it? Correct me if I'm wrong, but I get the impression that this is something that Oleg hacked up in an hour and posted ~15 years ago, but that it's never seen much use. Also, I'm personally not fond of the API, which is quite unconventional. The thing about assertion macros is that (1) they are usually trivial, and (2) I'm not aware of any consensus in the Scheme community on what a non-trivial assertion API should look like. I suspect this is why there's no SRFI for assertion macros, despite the fact that anyone can write a SRFI. For those who find the R6RS 'assert' macro unsatisfactory, I'm inclined to suggest that each project should feel free to define their own assertion macro according to their needs and preferences. Having said that, if you think you know a non-trivial assert API that other Schemers would like to use, feel free to publish a library or write a SRFI. If it becomes popular, we could consider including it with Guile. Regards, Mark