unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* new match system bug?
@ 2010-09-04 12:38 Stefan Israelsson Tampe
  2010-09-04 12:44 ` Ludovic Courtès
  0 siblings, 1 reply; 6+ messages in thread
From: Stefan Israelsson Tampe @ 2010-09-04 12:38 UTC (permalink / raw)
  To: guile-devel

[-- 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")

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: new match system bug?
  2010-09-04 12:38 new match system bug? Stefan Israelsson Tampe
@ 2010-09-04 12:44 ` Ludovic Courtès
  2010-09-04 13:29   ` Stefan Israelsson Tampe
  2010-09-04 15:02   ` Stefan Israelsson Tampe
  0 siblings, 2 replies; 6+ messages in thread
From: Ludovic Courtès @ 2010-09-04 12:44 UTC (permalink / raw)
  To: guile-devel

Hello!

Stefan Israelsson Tampe <stefan.tampe@spray.se> writes:

> 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:

  scheme@(guile-user)> (match '(a b) ((and x (a . b)) a))
  $1 = a

According to the grammar in the manual, I don’t think literal ‘...’ can
be used in the middle of a list; it should only be used at the end of a
list, where it means “zero or more”:

  scheme@(guile-user)> (match '(a b) ((a ...) a))
  $2 = (a b)

Thanks,
Ludo’.




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: new match system bug?
  2010-09-04 12:44 ` Ludovic Courtès
@ 2010-09-04 13:29   ` Stefan Israelsson Tampe
  2010-09-04 15:00     ` Ludovic Courtès
  2010-09-04 15:02   ` Stefan Israelsson Tampe
  1 sibling, 1 reply; 6+ messages in thread
From: Stefan Israelsson Tampe @ 2010-09-04 13:29 UTC (permalink / raw)
  To: guile-devel

On Saturday, September 04, 2010 02:44:49 pm Ludovic Courtès wrote:
> Hello!
> 
> Stefan Israelsson Tampe <stefan.tampe@spray.se> writes:
> > 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:
> 
>   scheme@(guile-user)> (match '(a b) ((and x (a . b)) a))
>   $1 = a
> 
> According to the grammar in the manual, I don’t think literal ‘...’ can
> be used in the middle of a list; it should only be used at the end of a
> list, where it means “zero or more”:
> 
>   scheme@(guile-user)> (match '(a b) ((a ...) a))
>   $2 = (a b)
> 
> Thanks,
> Ludo’.

How true, It's just that the code we took has an extension with a Bug in it.
It was intended to work for that case as well.

From match.upstream.scm:
;; 2007/07/21 - allowing ellipse patterns in non-final list positions

They just stopped variants of
the form (a ... b ...) cause it led to higher order matchers that could be
avoided by another means me thinks. 

So either.
1. we allow for this extension. or
2. Add a nice error message explaining that that feature is not suported.
   Right now - this is a Bug :-)

Note. I would like to allow multiple a ... patterns but warn against it use 
and change the algorithm. The reason is that there exists a common pattern 
idiom. Namely ( a a a a - b b b b b - c c c c c)  e.g. using separators. 
essentially it is very kind to allow people to do:

   (match list ((x ... '- . l) (begin (handle x) (f l))))

So you essentially allow a anti gready ... operator. Note here that using <x> 
abstractions althogh it works for the above example, it cannot reproduce a non
gready operator. For that you would need the continuation inside the match
abstractions.

Cheers
Stefan







^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: new match system bug?
  2010-09-04 13:29   ` Stefan Israelsson Tampe
@ 2010-09-04 15:00     ` Ludovic Courtès
  2010-09-04 15:17       ` Stefan Israelsson Tampe
  0 siblings, 1 reply; 6+ messages in thread
From: Ludovic Courtès @ 2010-09-04 15:00 UTC (permalink / raw)
  To: guile-devel

Hi Stefan!

Stefan Israelsson Tampe <stefan.tampe@spray.se> writes:

> How true, It's just that the code we took has an extension with a Bug in it.
> It was intended to work for that case as well.
>
> From match.upstream.scm:
> ;; 2007/07/21 - allowing ellipse patterns in non-final list positions

Then how about reporting it upstream Cc: guile-devel?  :-)

Thanks,
Ludo’.




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: new match system bug?
  2010-09-04 12:44 ` Ludovic Courtès
  2010-09-04 13:29   ` Stefan Israelsson Tampe
@ 2010-09-04 15:02   ` Stefan Israelsson Tampe
  1 sibling, 0 replies; 6+ messages in thread
From: Stefan Israelsson Tampe @ 2010-09-04 15:02 UTC (permalink / raw)
  To: guile-devel

[-- Attachment #1: Type: Text/Plain, Size: 770 bytes --]

On Saturday, September 04, 2010 02:44:49 pm Ludovic Courtès wrote:
> Hello!
> 
> Stefan Israelsson Tampe <stefan.tampe@spray.se> writes:
> > 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:
> 
>   scheme@(guile-user)> (match '(a b) ((and x (a . b)) a))
>   $1 = a
> 
> According to the grammar in the manual, I don’t think literal ‘...’ can
> be used in the middle of a list; it should only be used at the end of a
> list, where it means “zero or more”:
> 
>   scheme@(guile-user)> (match '(a b) ((a ...) a))
>   $2 = (a b)
> 
> Thanks,
> Ludo’.

I Included a trailing pattern error message to match.scm in this patch.
/Stefan

[-- Attachment #2: match-trailing.patch --]
[-- Type: text/x-patch, Size: 1808 bytes --]

diff --git a/module/ice-9/match.scm b/module/ice-9/match.scm
index 1a2a61e..3a709a1 100644
--- a/module/ice-9/match.scm
+++ b/module/ice-9/match.scm
@@ -115,6 +115,36 @@
       ((_ newpat  m ()            v kt ke i)
        (syntax (match-one v newpat () kt ke i))))))
 
+;;error messag added
+(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
+      (match-syntax-error
+       "trailing pattern to ... , eg. (x ... y) is not supported")))))
+
 ;;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 +271,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")

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: new match system bug?
  2010-09-04 15:00     ` Ludovic Courtès
@ 2010-09-04 15:17       ` Stefan Israelsson Tampe
  0 siblings, 0 replies; 6+ messages in thread
From: Stefan Israelsson Tampe @ 2010-09-04 15:17 UTC (permalink / raw)
  To: guile-devel

On Saturday, September 04, 2010 05:00:31 pm Ludovic Courtès wrote:
> Hi Stefan!
> 
> Stefan Israelsson Tampe <stefan.tampe@spray.se> writes:
> > How true, It's just that the code we took has an extension with a Bug in
> > it. It was intended to work for that case as well.
> > 
> > From match.upstream.scm:
> > ;; 2007/07/21 - allowing ellipse patterns in non-final list positions
> 
> Then how about reporting it upstream Cc: guile-devel?  :-)
> 
> Thanks,
> Ludo’.

I'll have a chat with the people at #scheme



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2010-09-04 15:17 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-09-04 12:38 new match system bug? Stefan Israelsson Tampe
2010-09-04 12:44 ` 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

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).