From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Stefan Israelsson Tampe Newsgroups: gmane.lisp.guile.devel Subject: Re: regex-case Date: Mon, 8 Feb 2016 17:15:14 +0100 Message-ID: References: <61E420AD-70B6-4DEA-A7DD-EB123E22EFD0@verizon.net> <87oabrgtrg.fsf@gnu.org> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=001a114444e08d62a4052b448126 X-Trace: ger.gmane.org 1454948284 19422 80.91.229.3 (8 Feb 2016 16:18:04 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 8 Feb 2016 16:18:04 +0000 (UTC) To: guile-devel Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Mon Feb 08 17:18:04 2016 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1aSoVj-0007WJ-4t for guile-devel@m.gmane.org; Mon, 08 Feb 2016 17:17:59 +0100 Original-Received: from localhost ([::1]:46210 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aSoVi-00018M-Ck for guile-devel@m.gmane.org; Mon, 08 Feb 2016 11:17:58 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:57143) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aSoT7-0005tR-O9 for guile-devel@gnu.org; Mon, 08 Feb 2016 11:15:19 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aSoT5-0002OL-SV for guile-devel@gnu.org; Mon, 08 Feb 2016 11:15:17 -0500 Original-Received: from mail-wm0-x22a.google.com ([2a00:1450:400c:c09::22a]:33138) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aSoT5-0002O5-Dr for guile-devel@gnu.org; Mon, 08 Feb 2016 11:15:15 -0500 Original-Received: by mail-wm0-x22a.google.com with SMTP id g62so139587381wme.0 for ; Mon, 08 Feb 2016 08:15:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=omVQEZXGbqkijlMDaDGuzP8YqyjFYa1vdgZmddjZrbo=; b=NAypyIhqq3bUda3tNw6++k5F/UZfrtD59NKW0lPyTbyoDlUIhWaqMmiRzadQWHx2DX qPfQsF0WcoqZ6Hwbq+ff03dEk9/V1mDttZdtHxQ3IxycmPesfLpHwGJg7VexUfPu/Q2X jLP5EEWW5w1BNL2let1Zy/eMG7/b1aPI0JdhjbqU2LmLPdZKzCNrXyCUxMfDL/ds9Gws IM92CCebcoXvLa1U03Kmhf2DKkfVViMDaO4gmTtPn2HrUjE6YiYnPfZK74xkM1d9n3N8 fXgdg3hd6vCCrfSm3HDVW13DngUDffK706Tr3QYWEQ7wcV3Trf79NQGIqaJG0mm2fc9l w0Yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type; bh=omVQEZXGbqkijlMDaDGuzP8YqyjFYa1vdgZmddjZrbo=; b=Oq6SSJRqVA0jF+zl2XsugLeDvQhfB0PZO/wZHFZrdSEKfVttf5a8NtckdynwOgp6T4 G3vAdzy636h33m66OwtA1lbwnRK3TRY3q9j+hDzy4ALJGWxzF+1nrYfC8pJkVkpfYlMd TV9Xj0/cZhfeFqtPx+//fl0xcG022NKtIZ7KnMhXd2ypjkENIeLifLl0dmP+s1GcH5io 8DzhgEB3Ij28wkMq6QX053Pum8DGEsAuXYA2vU/z13n+ekpdyyL82UHQD9KOQqB4sLxT w8DL8HbivEuwH6d2hLh8eb6fU42PDm3VGrGS26Oi3o2msyp3p1nxCBcsRV8IfrI6HB1+ dEMg== X-Gm-Message-State: AG10YOS5BwMllMdhC4FGKa/mbGFjh1dr0Q0y1k/b7riO+bk5x2Udwlce5fC3BSPggXN6Z4zBdUR02HKEsBsBKg== X-Received: by 10.28.174.72 with SMTP id x69mr14119762wme.68.1454948114848; Mon, 08 Feb 2016 08:15:14 -0800 (PST) Original-Received: by 10.27.187.196 with HTTP; Mon, 8 Feb 2016 08:15:14 -0800 (PST) In-Reply-To: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::22a X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Original-Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.devel:18152 Archived-At: --001a114444e08d62a4052b448126 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Because forwarded mail get hidden in modern mail clients I paste it here as well. Sorry for the spam An interesting question is what can be done by ice-9/match Consider (define (r1 x) (regexp-exec (make-regexp x))) Then simple matching can be done like (match x ((? (r1 "a+")) 'matche_a_lot_of_as) ((? (r1 "b+")) 'matche_a_lot_of_bs) ...) If we assume that the match =3D clause catches exceptiones we could use (define (r2 x) (let ((res (r1 x))) (if r1 r1 (throw match-error)))) Then defining (define (nth i) (lambda (x) (match:substring x i))) we can then do (match x ((=3D (r2 "^([a-z]+)\\(([0-9]+)\\$")) (and (=3D (nth 1) v) (=3D (nth 2) = i))) (list v i)) ...) Not as elegant as match-case but it is well composable. Another posibility is to define, i make this as simple as possible (the n argument can be removed) (define (r3 x n) (let ((res (r1 x))) (if res (let lp ((out '()) (i n)) (if (> n 0) (lp (cons (match:substring res i) out) (- i 1)) (reverse out))) (throw match-error)))) Then this should enable a quite nice match as (match x ((=3D (r3 "^([a-z]+)\\(([0-9]+)\\$" 2) (v i)) (list v i)) ...) which is not too bad. Now ice-9 match does not do the nessesary checks on throw and there is quite a bit of over head in the exception mechanisms but in principle we should be able to design an ice-9 match and an inlinable r1, r2, r3 so that the code shoulud be effective compilied and the exceptions transformed into goto's. I would like to, like for records, add support in upstream for catching a designated exception which shouled mean fail. What we could ask for is for foof to have customable macros like for slot-ref etc. e.g. (catch-error-object var thunk sucess-code failure-code) with default definition beeing (let ((var (thunk) sucees-code) and our definition could be something like (let ((res (catch match-error (lambda () (thunk)) (lambda x err-obj)))) (if (eq? res err-obj) failure-code (let ((var res)) success-code)))) It is not clean, but we can clobber the default macro definition previously defined in upstream to introduce our macro without changing upstream. When this is ready we could think about improving the compiler to expand regexp matches effectively. WDYT On Mon, Feb 8, 2016 at 4:40 PM, Stefan Israelsson Tampe < stefan.itampe@gmail.com> wrote: > > ---------- Forwarded message ---------- > From: Stefan Israelsson Tampe > Date: Mon, Feb 8, 2016 at 4:31 PM > Subject: Re: regex-case > To: Ludovic Court=C3=A8s > > > An interesting question is what can be done by ice-9/match > > Consider > (define (r1 x) (regexp-exec (make-regexp x))) > > Then simple matching can be done like > > (match x > ((? (r1 "a+")) 'matche_a_lot_of_as) > ((? (r1 "b+")) 'matche_a_lot_of_bs) > ...) > > If we assume that the match =3D clause catches exceptiones we could use > (define (r2 x) > (let ((res (r1 x))) > (if r1 > r1 > (throw match-error)))) > > Then defining > > (define (nth i) > (lambda (x) (match:substring x i))) > > we can then do > > (match x > ((=3D (r2 "^([a-z]+)\\(([0-9]+)\\$")) (and (=3D (nth 1) v) (=3D (nth 2= ) i))) > (list v i)) > ...) > > Not as elegant as match-case but it is well composable. > > Another posibility is to define, i make this as simple as possible (the n > argument can be removed) > (define (r3 x n) > (let ((res (r1 x))) > (if res > (let lp ((out '()) (i n)) > (if (> n 0) > (lp (cons (match:substring res i) out) (- i 1)) > (reverse out))) > (throw match-error)))) > > Then this should enable a quite nice match as > > (match x > ((=3D (r3 "^([a-z]+)\\(([0-9]+)\\$" 2) (v i)) > (list v i)) > ...) > > which is not too bad. Now ice-9 match does not do the nessesary checks on > throw and there is quite a bit of over head in the exception mechanisms b= ut > in principle we should be able to design an ice-9 match and an inlinable > r1, r2, r3 so that the code shoulud be effective compilied and the > exceptions transformed > into goto's. I would like to, like for records, add support in upstream > for catching a designated exception which shouled mean fail. What we coul= d > ask for is > for foof to have customable macros like for slot-ref etc. e.g. > > (catch-error-object var thunk sucess-code failure-code) > > with default definition beeing > (let ((var (thunk) sucees-code) > > and our definition could be something like > (let ((res (catch match-error > (lambda () (thunk)) > (lambda x err-obj)))) > (if (eq? res err-obj) > failure-code > (let ((var res)) > success-code)))) > > It is not clean, but we can clobber the default macro definition > previously defined in upstream to introduce our macro without changing > upstream. > When this is ready we could think about improving the compiler to expand > regexp matches effectively. > > WDYT > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > On Mon, Feb 8, 2016 at 3:29 PM, Ludovic Court=C3=A8s wrote= : > >> Matt Wette skribis: >> >> > (regex-case str >> > (("^([a-z]+)\\(([0-9]+)\\)$" v i) >> > (list v i)) >> > (("^([a-z]+)$" v) >> > (list v "1=E2=80=9D))) >> >> Sounds useful and convenient! >> >> > (let ((t-292 (make-regexp "^([a-z]+)\\(([0-9]+)\\)$")) >> > (t-293 (make-regexp "^([a-z]+)$"))) >> > (cond ((regexp-exec t-292 str) >> > =3D> >> > (lambda (m) >> > (let ((v (match:substring m 1)) >> > (i (match:substring m 2))) >> > (list v i)))) >> > ((regexp-exec t-293 str) >> > =3D> >> > (lambda (m) >> > (let ((v (match:substring m 1))) (list v "1")))))) >> >> When the =E2=80=98else=E2=80=99 clause is missing, I think it would be b= est to throw an >> error like =E2=80=98match=E2=80=99 does=E2=80=94it=E2=80=99s rarely help= ful to return #unspecified in >> those cases. >> >> Ludo=E2=80=99. >> >> >> > > --001a114444e08d62a4052b448126 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Because forwarded mail get hidden in modern mail clients I= paste it here as well. Sorry for the spam
An interesting question is what can be done by ice-9/match
= Consider
(define (r1 x) (regexp-exec= (make-regexp x)))

Then simple matching can be done like

(m= atch x
=C2=A0 =C2=A0((? (r1 "a+= ")) =C2=A0'matche_a_lot_of_as)
=C2=A0 =C2=A0((? (r1 "b+")) =C2=A0'matche_a_lot_of_bs)
=C2=A0 =C2=A0 ...)

If we assum= e that the match =3D clause catches exceptiones we could use
(define (r2 x)
=C2=A0 =C2=A0(let ((res (r1 x)))
=C2=A0 =C2=A0 =C2=A0 =C2=A0(if r1
= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0r1
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(throw match-error))))

Then defining

(define (nth i)
=C2=A0 =C2=A0 (lambda (x) (match:substring x i)))

we can the= n do

(match x
=C2=A0 =C2=A0(= (=3D (r2=C2=A0"^([a-z]+)\\(([0-9]+)\\= $")) (and (=3D (nth 1) v) (=3D (nth 2) i)))
=C2=A0 =C2=A0 (li= st v i))
=C2=A0 =C2=A0 ...)

Not as= elegant as match-case but it is well composable.

Another posibility is to = define, i make this as simple as possible (the n argument can be removed)
(define (r3 x n)
=C2=A0 =C2=A0(let ((res (r1 x)))
=C2=A0 =C2=A0 =C2=A0 =C2=A0(if res
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (let lp ((out '()) (i n))
= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 (if (> n 0)=C2=A0
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (lp (con= s (match:substring res i) out) (- i 1))
= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (reve= rse out)))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 (throw match-error))))
Then this should enable a quite nice m= atch as

(match x
=C2=A0 =C2=A0((=3D (r3= =C2=A0=C2=A0"^([a-z]+)\= \(([0-9]+)\\$" 2) (v i))
= =C2=A0 =C2=A0 (list v i))
=C2=A0 =C2=A0...)

which is not t= oo bad. Now ice-9 match does not do the nessesary checks on throw and there= is quite a bit of over head in the exception mechanisms but
in princip= le we should be able to design an ice-9 match and an inlinable r1, r2, r3 s= o that the code shoulud be effective compilied and the exceptions transform= ed
into goto's. I would like to, like for records, add support in u= pstream for catching a designated exception which shouled mean fail. What w= e could ask for is
for foof to have customable macros like for slot-ref= etc. e.g.

(catch-error-object var thu= nk sucess-code failure-code)

with default definition = beeing
(let ((var (thunk) sucees-cod= e)

and our definition could be something like
(let ((res (catch match-error
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0(lambda () (thunk))
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(l= ambda x err-obj))))
=C2=A0 =C2=A0 (i= f (eq? res err-obj)
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 failure-code
=C2=A0 = =C2=A0 =C2=A0 =C2=A0 (let ((var res))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 success-code))))

It is = not clean, but we can clobber the default macro definition previously defin= ed in upstream to introduce our macro without changing upstream.
When this is ready we could think about improv= ing the compiler to expand regexp matches effectively.

WDYT


On Mon, Feb 8, 2016 at 4:40 PM, Stefan Israelsson Tampe <s= tefan.itampe@gmail.com> wrote:

---------- Forwarded message ----------
From: Stefan Israelsson Tampe <stefan.itampe@gma= il.com>
Date: Mon, Feb 8, 2016 at 4:31 PM
Subject: Re: = regex-case
To: Ludovic Court=C3=A8s <ludo@gnu.org>


An inter= esting question is what can be done by ice-9/match

Consi= der
(define (r1 x) (regexp-exec (make-regexp x)))

<= /div>
Then simple matching can be done like

(m= atch x
=C2=A0 =C2=A0((? (r1 "a+")) =C2=A0'matche_a_= lot_of_as)
=C2=A0 =C2=A0((? (r1 "b+")) =C2=A0'match= e_a_lot_of_bs)
=C2=A0 =C2=A0 ...)

If we = assume that the match =3D clause catches exceptiones we could use
(define (r2 x)
=C2=A0 =C2=A0(let ((res (r1 x)))
=C2=A0= =C2=A0 =C2=A0 =C2=A0(if r1
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0r1
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(throw match-er= ror))))

Then defining

(de= fine (nth i)
=C2=A0 =C2=A0 (lambda (x) (match:substring x i)))

we can then do

(match x
=C2=A0 =C2=A0((=3D (r2=C2=A0"^([a-z]+)\\(([0-9]+)\\$")) (and (=3D (nth 1) v) (=3D (nth 2) i)))
<= span style=3D"color:rgb(80,0,80);font-size:12.8px">=C2=A0 =C2=A0 (list v i)= )
=C2= =A0 =C2=A0 ...)

Not as elegant as match-case but it is well composable.

Another posibility is to define, i make this as simple as possi= ble (the n argument can be removed)
(define (r3 x n)
=C2=A0 =C2= =A0(let ((res (r1 x)))
=C2=A0 =C2=A0 =C2=A0 =C2=A0(if res
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (let lp ((out '()) (i n))=
= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (if (> n 0)=C2= =A0
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 (lp (cons (match:substring res i) out) (- i 1))
<= div>=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (reverse out)))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (throw match-error))))<= /font>
<= br>
Then this should enable a quite nice match as
=

(m= atch x
=C2=A0 =C2=A0((=3D (r3=C2=A0=C2=A0"^([a-z]+)\\(([0-9]+)\\$" 2) (v i))<= /div>
=C2=A0 =C2=A0= (list v i))
=C2=A0 =C2=A0...)

which is not too bad. Now ice-9 match does not do the nes= sesary checks on throw and there is quite a bit of over head in the excepti= on mechanisms but
in principle we should be able to design an ice-9 match and an = inlinable r1, r2, r3 so that the code shoulud be effective compilied and th= e exceptions transformed
into goto's. I would like to, like for records, add = support in upstream for catching a designated exception which shouled mean = fail. What we could ask for is
for foof to have customable macros like for slot-r= ef etc. e.g.

(catch-error-object var thunk sucess-code failure-code)

with default definition beeing
(le= t ((var (thunk) sucees-code)

and our definition co= uld be something like
(let ((res (catch match-error
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(l= ambda () (thunk))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(lambda x err-obj))))
=C2=A0 =C2= =A0 (if (eq? res err-obj)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 failure-cod= e
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (let ((var res))
=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 success-code))))

It is not clean, but we can clobber the default macro definition previous= ly defined in upstream to introduce our macro without changing upstream.
When this is ready we could think about improving the compiler to e= xpand regexp matches effectively.

WDYT
<= br>






<= /font>
<= br>


=

=
=





=C2=A0=C2=A0


<= div>




<= div>



=

On Mon, Feb 8, 20= 16 at 3:29 PM, Ludovic Court=C3=A8s <ludo@gnu.org> wrote:
Matt Wette <matthew.wette@verizon.net> skribis:
>=C2=A0 (regex-case str
>=C2=A0 =C2=A0 (("^([a-z]+)\\(([0-9]+)\\)$" v i)
>=C2=A0 =C2=A0 =C2=A0(list v i))
>=C2=A0 =C2=A0 (("^([a-z]+)$" v)
>=C2=A0 =C2=A0 =C2=A0(list v "1=E2=80=9D)))

Sounds useful and convenient!

> (let ((t-292 (make-regexp "^([a-z]+)\\(([0-9]+)\\)$"))
>=C2=A0 =C2=A0 =C2=A0 =C2=A0(t-293 (make-regexp "^([a-z]+)$"))= )
>=C2=A0 =C2=A0(cond ((regexp-exec t-292 str)
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =3D>
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (lambda (m)
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (let ((v (match:substring m 1= ))
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (i (matc= h:substring m 2)))
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (list v i))))
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0((regexp-exec t-293 str)
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =3D>
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (lambda (m)
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (let ((v (match:substring m 1= ))) (list v "1"))))))

When the =E2=80=98else=E2=80=99 clause is missing, I think it would = be best to throw an
error like =E2=80=98match=E2=80=99 does=E2=80=94it=E2=80=99s rarely helpful= to return #unspecified in
those cases.

Ludo=E2=80=99.





--001a114444e08d62a4052b448126--