|
|
 |
 |
 |
 |
Scheme Programming Language
|
 |
 |
 |
 |
 |
 |
 |
 |
Pattern Matching for Guile
Jenia <evgeni.geni @gmail.com> writes: > Hello! > I'm looking for a pattern matching library (or its analogue) for > Guile. There is some one library for Bigloo ( http://www-sop.inria.fr/ > mimosa/fp/Bigloo/doc/bigloo-7.html), and several other implementations > ( http://schemecookbook.org/Cookbook/PatternMatchingChapter), but I > can't find any implementation for Guile. Is it any implementaton of > pattern matching for Guile? jas-match ([0], linked from the cookbook) should run on Guile, since it is R5RS-only AFAIK. [0] http://www.scheme.dk/src/match/jas-match.scm HTH, Rotty -- Andreas Rottmann | Rotty@ICQ | 118634484@ICQ | a.rottm@gmx.at http://rotty.uttx.net | GnuPG Key: http://rotty.uttx.net/gpg.asc Fingerprint | C38A 39C5 16D7 B69F 33A3 6993 22C8 27F7 35A9 92E7 v2sw7MYChw5pr5OFma7u7Lw2m5g/l7Di6e6t5BSb7en6g3/5HZa2Xs6MSr1/2p7 hackerkey.com I have a truly elegant proof of the above, but it is too long to fit into this .signature file.
Thank you! I have found jas-match here - http://mumble.net/~campbell/scheme/ (http://www.scheme.dk/src/match/jas-match.scm doesnt response). And I have got the following error: guile> (load "jas-match.scm") ERROR: invalid syntax () ABORT: (misc-error) Do you have any idea what is wrong? On May 23, 2:05 pm, Andreas Rottmann <a.rottm@gmx.at> wrote:
> Jenia <evgeni.geni @gmail.com> writes: > > Hello! > > I'm looking for a pattern matching library (or its analogue) for > > Guile. There is some one library for Bigloo ( http://www-sop.inria.fr/ > > mimosa/fp/Bigloo/doc/bigloo-7.html), and several other implementations > > ( http://schemecookbook.org/Cookbook/PatternMatchingChapter), but I > > can't find any implementation for Guile. Is it any implementaton of > > pattern matching for Guile? > jas-match ([0], linked from the cookbook) should run on Guile, since > it is R5RS-only AFAIK. > [0]http://www.scheme.dk/src/match/jas-match.scm > HTH, Rotty > -- > Andreas Rottmann | Rotty@ICQ | 118634484@ICQ | a.rottm@gmx.athttp://rotty.uttx.net | GnuPG Key:http://rotty.uttx.net/gpg.asc > Fingerprint | C38A 39C5 16D7 B69F 33A3 6993 22C8 27F7 35A9 92E7 > v2sw7MYChw5pr5OFma7u7Lw2m5g/l7Di6e6t5BSb7en6g3/5HZa2Xs6MSr1/2p7 hackerkey.com > I have a truly elegant proof of the above, but it is too long to > fit into this .signature file.
Thank you! I have found jas-match here - http://mumble.net/~campbell/scheme/ (http://www.scheme.dk/src/match/jas-match.scm doesnt response). And I have got the following error: guile> (load "jas-match.scm") ERROR: invalid syntax () ABORT: (misc-error) Do you have any idea what is wrong? On May 23, 2:05 pm, Andreas Rottmann <a.rottm@gmx.at> wrote:
> Jenia <evgeni.geni @gmail.com> writes: > > Hello! > > I'm looking for a pattern matching library (or its analogue) for > > Guile. There is some one library for Bigloo ( http://www-sop.inria.fr/ > > mimosa/fp/Bigloo/doc/bigloo-7.html), and several other implementations > > ( http://schemecookbook.org/Cookbook/PatternMatchingChapter), but I > > can't find any implementation for Guile. Is it any implementaton of > > pattern matching for Guile? > jas-match ([0], linked from the cookbook) should run on Guile, since > it is R5RS-only AFAIK. > [0]http://www.scheme.dk/src/match/jas-match.scm > HTH, Rotty > -- > Andreas Rottmann | Rotty@ICQ | 118634484@ICQ | a.rottm@gmx.athttp://rotty.uttx.net | GnuPG Key:http://rotty.uttx.net/gpg.asc > Fingerprint | C38A 39C5 16D7 B69F 33A3 6993 22C8 27F7 35A9 92E7 > v2sw7MYChw5pr5OFma7u7Lw2m5g/l7Di6e6t5BSb7en6g3/5HZa2Xs6MSr1/2p7 hackerkey.com > I have a truly elegant proof of the above, but it is too long to > fit into this .signature file.
You probably have to: (use-modules (ice-9 syncase)) Regards, Rotty -- Andreas Rottmann | Rotty@ICQ | 118634484@ICQ | a.rottm@gmx.at http://rotty.uttx.net | GnuPG Key: http://rotty.uttx.net/gpg.asc Fingerprint | C38A 39C5 16D7 B69F 33A3 6993 22C8 27F7 35A9 92E7 v2sw7MYChw5pr5OFma7u7Lw2m5g/l7Di6e6t5BSb7en6g3/5HZa2Xs6MSr1/2p7 hackerkey.com Anonymous surfing? Use Tor: http://tor.eff.net
Thank you! There is some progress: now I get another error : guile> (load "jas-match.scm") ERROR: invalid syntax (syntax-rules () [ (_ var () success failure) (if (null? v ar) success failure) ] [ (_ var #t success failure) (if (eq? var #t) success fai lure) ] ...) ABORT: (misc-error) How do you think what is the problem this time? On May 23, 3:44 pm, Andreas Rottmann <a.rottm@gmx.at> wrote:
I guess its because guile doesn't understand square brackets. I'm sure there are better[1] pattern matching libraries for guile. Did you try google? http://www.google.com/search?client=opera&rls=en&q=pattern+matching+g... [1] A library using square brackets? Hmm.
On Wed, 23 May 2007, Jenia wrote: > Thank you! > There is some progress: now I get another error : > guile> (load "jas-match.scm") > ERROR: invalid syntax (syntax-rules () [ (_ var () success failure) > (if (null? v > ar) success failure) ] [ (_ var #t success failure) (if (eq? var #t) > success fai > lure) ] ...) > ABORT: (misc-error) > How do you think what is the problem this time? > On May 23, 3:44 pm, Andreas Rottmann <a.rottm@gmx.at> wrote: >> Jenia <evgeni.geni@gmail.com> writes: >>> Thank you! >>> I have found jas-match here - http://mumble.net/~campbell/scheme/ >>> (http://www.scheme.dk/src/match/jas-match.scmdoesnt response). >>> And I have got the following error: >>> guile> (load "jas-match.scm") >>> ERROR: invalid syntax () >>> ABORT: (misc-error) >>> Do you have any idea what is wrong? >> You probably have to: >> (use-modules (ice-9 syncase)) >> Regards, Rotty >> -- >> Andreas Rottmann | Rotty@ICQ | 118634484@ICQ | a.rottm@gmx.athttp://rotty.uttx.net | GnuPG Key:http://rotty.uttx.net/gpg.asc >> Fingerprint | C38A 39C5 16D7 B69F 33A3 6993 22C8 27F7 35A9 92E7 >> v2sw7MYChw5pr5OFma7u7Lw2m5g/l7Di6e6t5BSb7en6g3/5HZa2Xs6MSr1/2p7 hackerkey.com >> Anonymous surfing? Use Tor:http://tor.eff.net
Hi, On May 23, 8:59 pm, Jenia <evgeni.geni@gmail.com> wrote: > Thank you! > There is some progress: now I get another error : > guile> (load "jas-match.scm") > ERROR: invalid syntax (syntax-rules () [ (_ var () success failure) > (if (null? v > ar) success failure) ] [ (_ var #t success failure) (if (eq? var #t) > success fai > lure) ] ...) > ABORT: (misc-error) > How do you think what is the problem this time?
Guile doesn't support square brackets. If you just want the MATCH macro ignoring hygiene, and don't intend to use any other hygienic macros, then you can (use-modules (ice-9 match)). If you do want hygiene, you may be better off with http://synthcode.com/schene/match.scm or http://synthcode.com/schene/match-simple.scm which is much more featureful than jas-match and has already been tested on several implementations (I just checked and at least some basic examples work out of the box in Guile w/ syncase). -- Alex
Jenia skrev: Whew, I thought the server was down. The response it gave just means the document is gone. Fear not, it now points you to: <http://schemecookbook.org/Cookbook/MacroExampleASimplePatternMatcher> After you replace [] with () it should work as is. Cons: It is simple. Pros: It is simple to modify. -- Jens Axel Sgaard
I would like to get any pattern matching for Guile. I have a little expirience with Bigloo pattern matching (Bigloo (http://www- sop.inria.fr/ mimosa/fp/Bigloo/doc/bigloo-7.html)). For example, in Bigloo (match- case '(a 2) ((a ??-) #t )) -> #t (a ??-) is pattern that matches any list begining with 'a and having any (possibly empty) repetition of anything in a list : (??-). How it can be written for any pattern matching library for Guile? On May 23, 4:26 pm, Alex Shinn <alexsh@gmail.com> wrote:
> Hi, > On May 23, 8:59 pm, Jenia <evgeni.geni@gmail.com> wrote: > > Thank you! > > There is some progress: now I get another error : > > guile> (load "jas-match.scm") > > ERROR: invalid syntax (syntax-rules () [ (_ var () success failure) > > (if (null? v > > ar) success failure) ] [ (_ var #t success failure) (if (eq? var #t) > > success fai > > lure) ] ...) > > ABORT: (misc-error) > > How do you think what is the problem this time? > Guile doesn't support square brackets. > If you just want the MATCH macro ignoring hygiene, > and don't intend to use any other hygienic macros, > then you can (use-modules (ice-9 match)). > If you do want hygiene, you may be better off with > http://synthcode.com/schene/match.scm > or > http://synthcode.com/schene/match-simple.scm > which is much more featureful than jas-match and > has already been tested on several implementations > (I just checked and at least some basic examples > work out of the box in Guile w/ syncase). > -- > Alex
On May 23, 11:31 pm, Jenia <evgeni.geni@gmail.com> wrote: > [...] in Bigloo (match-case '(a 2) ((a ??-) #t )) -> #t > (a ??-) is pattern that matches any list begining with 'a and having > any (possibly empty) repetition of anything in a list : (??-). > How it can be written for any pattern matching library for Guile?
In both Guile's built-in (ice-9 match) module and the match.scm that I linked to you can use (match '(a 2) (('a x ...) x)) => (2) Note by default symbols are identifiers, and you want ' to explicitly match them as literals. Guile's module is an adapted version of Andrew Wright's original code at http://www.cs.indiana.edu/scheme-repository/code.match.html The tarball includes documentation. My match.scm is a portable and hygienic backwards compatible superset of that API (notably it allows so-called non-linear patterns). It produces better code in a few cases, but most of the time produces larger code, and isn't as friendly as it could be with error messages. However the implementation is straightforward and easy to extend. -- Alex
Thank you for cooperation. I did'n know that Guile has built-in module for pattern matching. Ok, my problem is very simple: I want to match list that has the head 'a and the tail contains list with the head 'b. This lists are valid: '(a (b)), '(a c (b) d), '(a e (b e r) r). How it could be written in Andrew Wright's pattern matching language? I have tried to write this: (match-lambda (('a ('b x ...) ... ) #t) ) but it doesnt work. In Bigloo pattern for it is (a ??- (b ???-) ???- ) . On May 23, 6:49 pm, Alex Shinn <alexsh@gmail.com> wrote:
> On May 23, 11:31 pm, Jenia <evgeni.geni @gmail.com> wrote: > > [...] in Bigloo (match-case '(a 2) ((a ??-) #t )) -> #t > > (a ??-) is pattern that matches any list begining with 'a and having > > any (possibly empty) repetition of anything in a list : (??-). > > How it can be written for any pattern matching library for Guile? > In both Guile's built-in (ice-9 match) module and the match.scm > that I linked to you can use > (match '(a 2) (('a x ...) x)) => (2) > Note by default symbols are identifiers, and you want ' to > explicitly match them as literals. > Guile's module is an adapted version of Andrew Wright's original > code at > http://www.cs.indiana.edu/scheme-repository/code.match.html > The tarball includes documentation. > My match.scm is a portable and hygienic backwards compatible > superset of that API (notably it allows so-called non-linear > patterns). It produces better code in a few cases, but most > of the time produces larger code, and isn't as friendly as it > could be with error messages. However the implementation is > straightforward and easy to extend. > -- > Alex
Hi, On May 24, 2:13 am, Jenia <evgeni.geni@gmail.com> wrote: > I did'n know that Guile has built-in module for pattern matching. > Ok, my problem is very simple: I want to match list that has the head > 'a and the tail contains list with the head 'b. This lists are valid: > '(a (b)), '(a c (b) d), '(a e (b e r) r). > How it could be written in Andrew Wright's pattern matching language? > I have tried to write this: (match-lambda (('a ('b x ...) ... ) #t) ) > but it doesnt work. > In Bigloo pattern for it is (a ??- (b ???-) ???- ) .
Implementations of Wright's MATCH typically only allow ellipses after the last pattern in a list or vector. I suspect the reason is because otherwise you could have two or more ellipses in a list, which would involve backtracking, specifically O(n^k) backtracking for k ellipses! So it makes it too easy to write extremely expensive patterns. Likewise SYNTAX-RULES makes the same restriction. One alternative is to allow trailing expressions but only allow one ellipse per list, so the final elements of a list can be confirmed without any backtracking. PLT's SYNTAX-RULES, for instance, allows a single pattern after an ellipse to match the last element. -- Alex
Thank you! But my peoblem is still opened. I don't fully understand Wright's pattern language. _ is anything, ... is zero or more. What is the pattern for this case for example: list (my-list) begins with 'a, and the tail of this list contains list that begins with 'b. (It means that my-list has the first symbol 'a and any after it and then has list that begins with 'b, and after this list anything can follow.) My-list could also be dotted-pair list. This list are my-lists: '(a (b)), '(a (b) (c)), '(a c (v) b d (b e r) c d), '(a (b c . d) r t . y) Could you please help me what pattern should be written for my purpose? My solution is (define my-list (match-lambda (('a ... ('b ...) ... ) #t) (else #f) ) ) but it doesn't work. On May 24, 3:15 pm, Alex Shinn <alexsh@gmail.com> wrote:
> Hi, > On May 24, 2:13 am, Jenia <evgeni.geni@gmail.com> wrote: > > I did'n know that Guile has built-in module for pattern matching. > > Ok, my problem is very simple: I want to match list that has the head > > 'a and the tail contains list with the head 'b. This lists are valid: > > '(a (b)), '(a c (b) d), '(a e (b e r) r). > > How it could be written in Andrew Wright's pattern matching language? > > I have tried to write this: (match-lambda (('a ('b x ...) ... ) #t) ) > > but it doesnt work. > > In Bigloo pattern for it is (a ??- (b ???-) ???- ) . > Implementations of Wright's MATCH typically only allow ellipses > after the last pattern in a list or vector. I suspect the reason is > because otherwise you could have two or more ellipses in a list, > which would involve backtracking, specifically O(n^k) backtracking > for k ellipses! So it makes it too easy to write extremely expensive > patterns. > Likewise SYNTAX-RULES makes the same restriction. One alternative > is to allow trailing expressions but only allow one ellipse per list, > so the final elements of a list can be confirmed without any > backtracking. PLT's SYNTAX-RULES, for instance, allows a single > pattern after an ellipse to match the last element. > -- > Alex
Jenia wrote: > Could you please help me what pattern should be written for my > purpose? > My solution is (define my-list (match-lambda (('a ... ('b ...) ... ) > #t) (else #f) ) ) but it doesn't work.
I think, this is impossible with "plain" patterns, but you can use ?-pattern: (match-lambda (('a . (? has-a-member-which-is-a-list-and-has-b-as-first _)) #t) (else #f)) Udv, s
I see, that Andrew Wright's pattern language is too restricted. And it is more simple to write custom code than implement some function using this pattern language. How the has-a-member-which-is-a-list-and-has-b-as-first function should be implemented using patterns? It is enought simple to write it in pure Scheme like this: (define (has-a-member-which-is-a-list-and-has-b-as-first ? lst) (any ; srfi-1 (lambda (item) (and (pair? item) (eq? 'b (car item)))) lst)) On May 24, 10:58 pm, szgyg <s@elte.hungary> wrote:
> Jenia wrote: > > Could you please help me what pattern should be written for my > > purpose? > > My solution is (define my-list (match-lambda (('a ... ('b ...) ... ) > > #t) (else #f) ) ) but it doesn't work. > I think, this is impossible with "plain" patterns, but you can use > ?-pattern: > (match-lambda > (('a . (? has-a-member-which-is-a-list-and-has-b-as-first _)) #t) > (else #f)) > Udv, > s
|
 |
 |
 |
 |
|