unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Stefan Israelsson Tampe <stefan.tampe@spray.se>
To: guile-devel@gnu.org
Subject: new match system bug?
Date: Sat, 4 Sep 2010 14:38:37 +0200	[thread overview]
Message-ID: <201009041438.37352.stefan.tampe@spray.se> (raw)

[-- Attachment #1: Type: text/plain, Size: 257 bytes --]

Hi,

While eating the dogfood of the new match macro I come across a bug
e.g, this does not work!

(match '(a b) ((and x (a ... b)) a))

But using the attached patch, it works!!

Note. This should be interesting for upstreams maintainers.

/Regards 
Stefan

[-- Attachment #2: match-gen-ellipses-bug.patch --]
[-- Type: text/x-patch, Size: 2497 bytes --]

diff --git a/module/ice-9/match.scm b/module/ice-9/match.scm
index 1a2a61e..b72d005 100644
--- a/module/ice-9/match.scm
+++ b/module/ice-9/match.scm
@@ -115,6 +115,53 @@
       ((_ newpat  m ()            v kt ke i)
        (syntax (match-one v newpat () kt ke i))))))
 
+;;Bug (match '(1 2) ((and x (a ... b))  b)) fails without the following fix
+(define-syntax match-gen-ellipses
+  (syntax-rules ()
+    ((_ v p () g+s (sk ...) fk i ((id id-ls) ...))
+     (match-check-identifier p
+       ;; simplest case equivalent to (p ...), just bind the list
+       (let ((p v))
+         (if (list? p)
+             (sk ... i)
+             fk))
+       ;; simple case, match all elements of the list
+       (let loop ((ls v) (id-ls '()) ...)
+         (cond
+           ((null? ls)
+            (let ((id (reverse id-ls)) ...) (sk ... i)))
+           ((pair? ls)
+            (let ((w (car ls)))
+              (match-one w p ((car ls) (set-car! ls))
+                         (match-drop-ids (loop (cdr ls) (cons id id-ls) ...))
+                         fk i)))
+           (else
+            fk)))))
+    ((_ v p r g+s (sk ...) fk i ((id id-ls) ...))
+     ;; general case, trailing patterns to match, keep track of the
+     ;; remaining list length so we don't need any backtracking
+     (match-verify-no-ellipses
+      r
+      (let* ((tail-len (length 'r))
+             (ls v)
+             (len (length ls)))
+        (if (< len tail-len)
+            fk
+            (let loop ((ls ls) (n len) (id-ls '()) ...)
+              (cond
+                ((= n tail-len)
+                 (let ((id (reverse id-ls)) ...)
+                   (match-one ls r (#f #f) (sk ...) fk i)))
+                ((pair? ls)
+                 (let ((w (car ls)))
+                   (match-one w p ((car ls) (set-car! ls))
+                              (match-drop-ids
+                               (loop (cdr ls) (- n 1) (cons id id-ls) ...))
+                              fk
+                              i)))
+                (else
+                 fk)))))))))
+
 ;;We must be able to extract vars in the new constructs!!
 (define-syntax match-extract-vars
   (syntax-rules (_ ___ *** ? $ = quote quasiquote and or not get! set!)
@@ -241,5 +288,5 @@
 		    #'(begin exp ...))))))
 
 (include-from-path/filtered
- (match-extract-vars match-two match)
- "ice-9/match.upstream.scm")
\ No newline at end of file
+ (match-gen-ellipses match-extract-vars match-two match)
+ "ice-9/match.upstream.scm")

             reply	other threads:[~2010-09-04 12:38 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-09-04 12:38 Stefan Israelsson Tampe [this message]
2010-09-04 12:44 ` new match system bug? Ludovic Courtès
2010-09-04 13:29   ` Stefan Israelsson Tampe
2010-09-04 15:00     ` Ludovic Courtès
2010-09-04 15:17       ` Stefan Israelsson Tampe
2010-09-04 15:02   ` Stefan Israelsson Tampe

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

  List information: https://www.gnu.org/software/guile/

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

  git send-email \
    --in-reply-to=201009041438.37352.stefan.tampe@spray.se \
    --to=stefan.tampe@spray.se \
    --cc=guile-devel@gnu.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).