public inbox for kawa@sourceware.org
 help / color / mirror / Atom feed
From: Per Bothner <per@bothner.com>
To: Sascha Ziemann <ceving@gmail.com>,
	kawa mailing list <kawa@sourceware.org>
Subject: Re: syntax-rules problem
Date: Fri, 10 Mar 2017 19:04:00 -0000	[thread overview]
Message-ID: <d229c021-f1d4-ac99-6625-eaa4b793e723@bothner.com> (raw)
In-Reply-To: <CAGUt3y5kG3x462cDKCH4VueT8HLZmcKTvyTZ3thWL-9dFpCvSQ@mail.gmail.com>

On 03/10/2017 01:35 AM, Sascha Ziemann wrote:
> I have the following macro.
>
> (define-syntax define-facts
>   (syntax-rules ()
>     ((_ (name a0 a1 ...) ((v00 v01 ...) (v10 v11 ...) ...))
>      '(define (name a0 a1 ...)
>         (conde
>           ((== a0 v00) (== a1 v01) ...)
>           ((== a0 v10) (== a1 v11) ...)
>           ...)))))

This got mangled a bit, but definition is clearly wrong.
I cleaned it up:

(define-syntax define-facts
   (syntax-rules ()
     ((_ (name arg ...) ((value key ...) ...))
      (define (name arg ...)
        (cond
         ((and (equal? arg key) ...) value)
         ...)))))

However, this is still not valid R7RS Scheme:

     Pattern variables that occur in subpatterns followed by one or more
     instances of the identifier h<ellipsis> are allowed only in subtemplates
     that are followed by as many instances of <ellipsis>.

The problem is that arg is singly-nested in the pattern, but
doubly-nested in the template.

Kawa does support an extension for different nesting levels, meant to support
things like (x (y ...) ...)  => (((x y) ...) ...).  However, in that case the x
varies by the outer "index":
((x0 (y00 y01)) (x1 (y10 y11 y12)))
  ==> (((x0 y00) (x0 y01)) ((x1 y10) (x1 y11) (x1 y12)))
But for define-facts the single-nested arg needs to vary with the
*inner* index. I.e. you would need Kawa to expand the example to:
  ==> (((x0 y00) (x1 y01)) ((x0 y10) (x1 y11) (x2 y12))) ; Oops no x2
But Kawa doesn't do that.

A solution that does work uses a list:

(define-syntax define-facts
   (syntax-rules ()
     ((_ name ((value . keys) ...))
      (define (name . args)
        (cond ((equal? args 'keys) value) ...)))))

(define-facts father-of
   (("Abraham" "Ismael")
    ("Abraham" "Isaac")
    ("Isaac"   "Jacob")
    ("Jacob"   "Benjamin")))

Obviously, Kawa should not throw ArrayIndexOutOfBoundsException,
but should instead report an error. So that is a Kawa bug.
-- 
	--Per Bothner
per@bothner.com   http://per.bothner.com/

  reply	other threads:[~2017-03-10 19:04 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-10  9:35 Sascha Ziemann
2017-03-10 19:04 ` Per Bothner [this message]
2017-03-10 20:11   ` Per Bothner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d229c021-f1d4-ac99-6625-eaa4b793e723@bothner.com \
    --to=per@bothner.com \
    --cc=ceving@gmail.com \
    --cc=kawa@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).