guile-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: match-abs


From: Stefan Israelsson Tampe
Subject: Re: match-abs
Date: Thu, 2 Sep 2010 20:07:04 +0200
User-agent: KMail/1.13.5 (Linux/2.6.34-12-desktop; KDE/4.4.4; x86_64; ; )

On Thursday, September 02, 2010 05:59:59 pm Ludovic Courtès wrote:
> Hi Stefan,
> 
> Stefan Israelsson Tampe <address@hidden> writes:
> > Anyway  consider a list [a a b b] and let <a> be a function so that
> > 
> > (<a> [a a b b]) -> (cons [a a] [b b])
> > (<b> [b b])     -> (cons [b b] [])
> > 
> > e.g. <a> macthes a sequence of a:s and <b> macthes a sequence of b:s. a
> > failure in this protocol is represented by the car of the retruning cons
> > beeing false.
> 
> OK.
> 
> > Note, we could use a plain multiple return values protocol but that is
> > for later discussion.
> > 
> > so using match-abs we would like the following
> > 
> > (match [a a b b] ((<a> <b>)  (append <b>.r <a>.r)))
> > 
> > to result in [b b a a].
> 
> OK, but...
> 
> In (ice-9 match), a pattern like ‘(a b)’ matches any 2-element list and
> binds the first element to ‘a’ and the second to ‘b’.

Yeah, the <> construct is much more into line with the idea of match. My 
point is that it can be a bit tough on the eyes to see the overall 
pattern and I was suggesting prepair things in a header so that we still
have some hygiene. But the header approach is more error prone to typos
so <> is good for typos and header is good for logical errors.
 
> To match 2-element lists where each element satisfies a certain
> predicate, say ‘p’, and bind the elements to ‘a’ and ‘b’, one must write
> ‘((and (? p) a) (and (? p) b))’.

I think that (? p a) may be enough work :-)

> The syntax you suggest here departs from this, right?

It has a pointwise compainon in
(= f (? id a)) 
if I'm not misstaken though.
(take  (define (f x) (match x ('a 'a) (_  #f))) and we have an obfuscation
of an 'a match ... oh well a is bound to a' :-))
        
> [...]
> 
> > One idea that is coded is to name the matchers and corresponding
> > variables that is bound to leading to the suggested
> 
> Parse error.  :-)
> 
> Could you rephrase this sentence?

Here comes an idea (implemented but not solid in match-abs). Consider 
describing a syntax element <a> in a header as beeing special so that it 
can stand by it self in the matcher without a surrounding context form.
purist may not like it but it helps in spotting logical errors in longer
matchers. Now this syntactic elements will bind a variable so let us use 
a header of the form ((<a> a1 a2) ...) to mean that in a matcher the result
of the first match <a> is bound to a1, the second to a2 etc. Let's call
this header abstractions and get a match for like ....

 
> > (match abstractions ((<a> <a>.r1 <a>.r2) (<b> <b>.r))
> > 
> >        [a a b b a a]
> >        ((<a> <b> <a>)   (append <b>.r <a>.r1 <a>.r2)))
> 
> What’s ‘abstractions’ here?  Is it the name of a variable, and if so
> what’s its value?  Is it a literal ‘abstraction’ interpreted as magic by
> the ‘match’ macro?

see above.

> > Note how we use <a> two times and the first time it is bound to <a>.r1
> > and the second time it's result is bound to <a>.r2.
> > 
> > Now putting some semmatics in a header can make the matchers themselves
> > look cleaner and save on vertical space. In match-abs there is an
> > alternative way of express this acording to
> > 
> > (match [a a b b a a]
> > 
> >        ([(<> <a> <a>.r1) (<> <b> <b>.r) (<> <a> <a>.r2)]
> >        
> >         (append <b>.r <a>.r1> <a>.r2)))
> 
> Hmm.  So IIUC, the sub-pattern ‘(<> <a> <a>.r1)’ matches anything that
> is a match according to custom matcher ‘<a>’, and binds the sub-match
> ‘r1’ of ‘<a>’ to ‘<a>.r1’, right?

yes

> Tricky...

This do the trick :-)

((match-two abs v ((<> (f ...) p) . l) g+s sk fk i)
     (let ((res (f ... v)))
       (if (car res)
           (match-one abs (car res) g+s 
                      (match-one (cdr res) l g+s sk fk)
                      fk i)
           (isert-abs abs fk))))

But now I think it should be 
((match-two abs v ((<> f p) . l) g+s sk fk i)
     (let ((res (f v)))
       (if (car res)
           (match-one abs (car res) g+s 
                      (match-one (cdr res) l g+s sk fk)
                      fk i)
           (isert-abs abs fk))))

and demand currying in the protocol!

> Thanks,
> Ludo’.

Have fun!
Stefan



reply via email to

[Prev in Thread] Current Thread [Next in Thread]