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: Fwd: regex-case Date: Mon, 8 Feb 2016 16:40:21 +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=047d7bb04ed6c615ec052b4404f9 X-Trace: ger.gmane.org 1454946030 11844 80.91.229.3 (8 Feb 2016 15:40:30 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 8 Feb 2016 15:40:30 +0000 (UTC) To: guile-devel Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Mon Feb 08 16:40:30 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 1aSnvR-0002t4-4N for guile-devel@m.gmane.org; Mon, 08 Feb 2016 16:40:29 +0100 Original-Received: from localhost ([::1]:45881 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aSnvQ-0007fA-Ld for guile-devel@m.gmane.org; Mon, 08 Feb 2016 10:40:28 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:47263) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aSnvL-0007eA-Rl for guile-devel@gnu.org; Mon, 08 Feb 2016 10:40:25 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aSnvK-0001PV-Ga for guile-devel@gnu.org; Mon, 08 Feb 2016 10:40:23 -0500 Original-Received: from mail-wm0-x232.google.com ([2a00:1450:400c:c09::232]:35880) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aSnvK-0001PO-5y for guile-devel@gnu.org; Mon, 08 Feb 2016 10:40:22 -0500 Original-Received: by mail-wm0-x232.google.com with SMTP id p63so119357609wmp.1 for ; Mon, 08 Feb 2016 07:40:22 -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=KdkDnbpKpjVtWXrH5JwgsNtBqMrApIQUnPtNVLUiv3Q=; b=ipN3+z3V6L65mVQrRB033y2iy2x5xB4tiICpL1enrvrXnoRnvLojUcpUznZLta5JLD 7dU36HeFI+kOh6ZDTtus0ShjUE2LeqqVStUKYqLPN+JKDX1ZQA4LA91SJ9pupVprkvrT hkbin9LDeny9TWWA5bO8zkjX5EYcneRhzlE+E1+LqQKB6PwXpxmPN26vEqdRoYbRlhBR amEPohnD42TH+8zcSV11godKGrBj9CCp2OUl1xxn0DzZSHYNVFlkWP+w5BqKfR3DB/VJ X/TjnPghfPy3hMi9VXHxRRbirSliaxO3/xQ1h5BKXHsF87uFptLhSEP2qq/5AF30V2iQ YLyg== 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=KdkDnbpKpjVtWXrH5JwgsNtBqMrApIQUnPtNVLUiv3Q=; b=BdL9cbLFvEIN47WoHfRsbxPRNsDXdfRRF+Q3YKKcEnY31wYwXmOM350pr6U82j/uav G2EK6ExwZtuIBIUdojfe0hikl4jIFpbvSrUejUCRQVKxixAHSdDqzQmxV7H6ti85bC9q 1m+bCdnnzs6ty5xEn/BMM+fEmcHn4PJrLmrTiDXBjL/Je2TFl6NJ6laA3f5pl1JczbUW t9aFN5x09Zeg1Zk5DSvA+xS/pNLmrRdI0Rxh4T7C+r9UXbDdfLgjMA5PJTmWM50s+gt6 vK+v3RaMqVzFbZPvTIyzrEr54bIsVKNB84ZCkpg9NJ080UWi0x45Oiz5OtiooK+YjxMR t4wQ== X-Gm-Message-State: AG10YOTLWtIU5SFHcxT1Dfzae+jWHpLda4jXoMFnca1Q7Dw6QgpACCMYwHNXlEEO8YGL93Vh/kysiK4hY2mFBw== X-Received: by 10.194.83.105 with SMTP id p9mr27787386wjy.179.1454946021412; Mon, 08 Feb 2016 07:40:21 -0800 (PST) Original-Received: by 10.27.187.196 with HTTP; Mon, 8 Feb 2016 07:40:21 -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::232 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:18150 Archived-At: --047d7bb04ed6c615ec052b4404f9 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable ---------- 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 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 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 be= st to throw an > error like =E2=80=98match=E2=80=99 does=E2=80=94it=E2=80=99s rarely helpf= ul to return #unspecified in > those cases. > > Ludo=E2=80=99. > > > --047d7bb04ed6c615ec052b4404f9 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

---------- Forwarded messag= e ----------
From: Stefan Israelsson Tampe= <stefa= n.itampe@gmail.com>
Date: Mon, Feb 8, 2016 at 4:31 PM
S= ubject: Re: regex-case
To: Ludovic Court=C3=A8s <ludo@gnu.org>


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

Consider
(define (r1 x) (regexp-exec (make-regexp x)))

<= div>Then simple matching can be done like

(match x=
=C2=A0 =C2=A0((? (r1 "a+")) =C2=A0'matche_a_lot_of= _as)
=C2=A0 =C2=A0((? (r1 "b+")) =C2=A0'matche_a_lo= t_of_bs)
=C2=A0 =C2=A0 ...)

If we assume= that the match =3D clause catches exceptiones we could use
(defi= ne (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=A0= r1
=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 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)))
=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 possible (th= e 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))
=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))))

=
= Then this should enable a quite nice match 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 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 mech= anisms but
in principle we should be able to design an ice-9 match and an inlinab= le r1, r2, r3 so that the code shoulud be effective compilied and the excep= tions transformed
into goto's. I would like to, like for records, add support= in upstream for catching a designated exception which shouled mean fail. W= hat 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
=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(lambda x err-obj))))
=C2=A0 =C2=A0 (if (= 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 no= t clean, but we can clobber the default macro definition previously defined= in upstream to introduce our macro without changing upstream.
Wh= en this is ready we could think about improving the compiler to expand rege= xp matches effectively.

WDYT

<= div>






=
=


=

=
=




=C2=A0=C2=A0












--047d7bb04ed6c615ec052b4404f9--