Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Save energy: Drive a smaller shell.


devel / comp.lang.scheme / macro return

SubjectAuthor
* macro returndamien...@gmail.com
`* Re: macro returnMarc Nieper-Wißkirchen
 `- Re: macro returndamien...@gmail.com

1
macro return

<d19b9851-12f2-406e-ad94-69f6cb264b4dn@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=57&group=comp.lang.scheme#57

  copy link   Newsgroups: comp.lang.scheme
X-Received: by 2002:ad4:5444:: with SMTP id h4mr22538624qvt.14.1624834912414; Sun, 27 Jun 2021 16:01:52 -0700 (PDT)
X-Received: by 2002:a05:6808:301:: with SMTP id i1mr14867490oie.144.1624834912146; Sun, 27 Jun 2021 16:01:52 -0700 (PDT)
Path: i2pn2.org!i2pn.org!aioe.org!feeder1.feed.usenet.farm!feed.usenet.farm!tr3.eu1.usenetexpress.com!feeder.usenetexpress.com!tr1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.scheme
Date: Sun, 27 Jun 2021 16:01:51 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=2a01:cb1d:2ea:2b00:1ccb:ef12:3978:8a93; posting-account=fsO9GQoAAABVrKpEsXBNjAuKdqx1JbN2
NNTP-Posting-Host: 2a01:cb1d:2ea:2b00:1ccb:ef12:3978:8a93
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <d19b9851-12f2-406e-ad94-69f6cb264b4dn@googlegroups.com>
Subject: macro return
From: damien.mattei@gmail.com (damien...@gmail.com)
Injection-Date: Sun, 27 Jun 2021 23:01:52 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 22
 by: damien...@gmail.com - Sun, 27 Jun 2021 23:01 UTC

hi,

i wanted to create a macro that is used like a function definition and allow return using call/cc:

(define-syntax def
(syntax-rules (return)
((_ (name args ...) body body* ...)
(define name (lambda (args ...)
(call/cc (lambda (return) body body* ...)))))
((_ name expr) (define name expr))))

unfortunaly i got error:

scheme@(guile-user)> (def (test x) (cond ((= x 3) 7) ((= x 2) (return 5)) (else 3)))
;;; <stdin>:2:42: warning: possibly unbound variable `return'
scheme@(guile-user)> (test 2)
ice-9/boot-9.scm:1685:16: In procedure raise-exception:
Unbound variable: return

any idea?

Damien

Re: macro return

<dbe607b4-478e-4a81-9245-faa52a3373e4n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=58&group=comp.lang.scheme#58

  copy link   Newsgroups: comp.lang.scheme
X-Received: by 2002:a05:620a:290a:: with SMTP id m10mr8459292qkp.190.1624859123232;
Sun, 27 Jun 2021 22:45:23 -0700 (PDT)
X-Received: by 2002:a9d:2649:: with SMTP id a67mr20053352otb.181.1624859122911;
Sun, 27 Jun 2021 22:45:22 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.scheme
Date: Sun, 27 Jun 2021 22:45:22 -0700 (PDT)
In-Reply-To: <d19b9851-12f2-406e-ad94-69f6cb264b4dn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2a02:810d:440:adc:7419:92ba:97f8:ec3b;
posting-account=zlMYeAoAAAA7h62AQsuRj9JlpjL_8Ob1
NNTP-Posting-Host: 2a02:810d:440:adc:7419:92ba:97f8:ec3b
References: <d19b9851-12f2-406e-ad94-69f6cb264b4dn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <dbe607b4-478e-4a81-9245-faa52a3373e4n@googlegroups.com>
Subject: Re: macro return
From: marc.nieper@gmail.com (Marc Nieper-Wißkirchen)
Injection-Date: Mon, 28 Jun 2021 05:45:23 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
 by: Marc Nieper-Wißkirc - Mon, 28 Jun 2021 05:45 UTC

Hi Damien,

first, a preliminary remark: that you have added the identifier `return' to the list of literals of the `syntax-rules' instance is useless; this would only be useful if `return' appeared in the patterns of the left-hand side of the `syntax-rules' rules.

Now to the problem: When a `syntax-rules' macro writes its output, identifiers not coming from the macro call are "renamed" so that they don't interfere with identifiers bound at the macro use site by accident. This is called "macro hygiene". For example,

(let ((return (lambda (x) 42)))
(def (test) (return 52))
(test))

evaluates to 42.

To write a macro like yours, you can either drop hygiene by injecting `return' in the syntactic environment of the macro call, or you can make `return' a syntax parameter.

The first solution is:

(define-syntax def
(lambda (x)
(syntax-case x ()
((k (name . arg*) body body* ...)
(identifier? #'name)
(with-syntax ((return (datum->syntax #'k 'return)))
#'(define (name . arg*) (call/cc (lambda (return) body body* ...)))))
((_ name expr)
(identifier? #'name)
#'(define name expr)))))
The second solution is hygienic but needs syntax parameters. You can find a full solution here: https://www.gnu.org/software/guile/manual/html_node/Syntax-Parameters.html.

Note that the semantics are different. In the first solution, `return' is lexically bound; in the second solution, `return' is not rebound, but its meaning changes during the expansion of the procedure body. Moreover, in the first solution, `return' is an ordinary procedure (a continuation, to be more precise); in the second, it is a syntactic keyword.

When the Scheme system has syntax parameters (like Guile), the second solution is probably the better one.

Marc

damien...@gmail.com schrieb am Montag, 28. Juni 2021 um 01:01:53 UTC+2:
> hi,
>
> i wanted to create a macro that is used like a function definition and allow return using call/cc:
>
> (define-syntax def
> (syntax-rules (return)
> ((_ (name args ...) body body* ...)
> (define name (lambda (args ...)
> (call/cc (lambda (return) body body* ...)))))
> ((_ name expr) (define name expr))))
>
> unfortunaly i got error:
>
> scheme@(guile-user)> (def (test x) (cond ((= x 3) 7) ((= x 2) (return 5)) (else 3)))
> ;;; <stdin>:2:42: warning: possibly unbound variable `return'
> scheme@(guile-user)> (test 2)
> ice-9/boot-9.scm:1685:16: In procedure raise-exception:
> Unbound variable: return
>
>
> any idea?
>
> Damien

Re: macro return

<7b1e3fc2-fa3e-489c-a461-90a981e31144n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=59&group=comp.lang.scheme#59

  copy link   Newsgroups: comp.lang.scheme
X-Received: by 2002:a05:6214:bc3:: with SMTP id ff3mr9749268qvb.49.1624866756390;
Mon, 28 Jun 2021 00:52:36 -0700 (PDT)
X-Received: by 2002:a05:6830:1352:: with SMTP id r18mr19846617otq.302.1624866756080;
Mon, 28 Jun 2021 00:52:36 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.scheme
Date: Mon, 28 Jun 2021 00:52:35 -0700 (PDT)
In-Reply-To: <dbe607b4-478e-4a81-9245-faa52a3373e4n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2a01:cb1d:2ea:2b00:7164:58cb:f109:9452;
posting-account=fsO9GQoAAABVrKpEsXBNjAuKdqx1JbN2
NNTP-Posting-Host: 2a01:cb1d:2ea:2b00:7164:58cb:f109:9452
References: <d19b9851-12f2-406e-ad94-69f6cb264b4dn@googlegroups.com> <dbe607b4-478e-4a81-9245-faa52a3373e4n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <7b1e3fc2-fa3e-489c-a461-90a981e31144n@googlegroups.com>
Subject: Re: macro return
From: damien.mattei@gmail.com (damien...@gmail.com)
Injection-Date: Mon, 28 Jun 2021 07:52:36 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
 by: damien...@gmail.com - Mon, 28 Jun 2021 07:52 UTC

thank Marc

i already use all that in the past (
https://github.com/damien-mattei/library-FunctProg/blob/master/syntactic-sugar.scm#L133 )
but it was late and i forget it

On Monday, June 28, 2021 at 7:45:24 AM UTC+2, marc....@gmail.com wrote:
> Hi Damien,
>
> first, a preliminary remark: that you have added the identifier `return' to the list of literals of the `syntax-rules' instance is useless; this would only be useful if `return' appeared in the patterns of the left-hand side of the `syntax-rules' rules.
>
right i have tested the two case wit or without return added to the list of literals ,it does not change, and thereislittle litterature about the use of literals in syntax-rules

> Now to the problem: When a `syntax-rules' macro writes its output, identifiers not coming from the macro call are "renamed" so that they don't interfere with identifiers bound at the macro use site by accident. This is called "macro hygiene". For example,
>
> (let ((return (lambda (x) 42)))
> (def (test) (return 52))
> (test))
>
> evaluates to 42.
>
> To write a macro like yours, you can either drop hygiene by injecting `return' in the syntactic environment of the macro call, or you can make `return' a syntax parameter.
>
> The first solution is:
>
> (define-syntax def
> (lambda (x)
> (syntax-case x ()
> ((k (name . arg*) body body* ...)
> (identifier? #'name)
> (with-syntax ((return (datum->syntax #'k 'return)))
> #'(define (name . arg*) (call/cc (lambda (return) body body* ...)))))
> ((_ name expr)
> (identifier? #'name)
> #'(define name expr)))))
>
> The second solution is hygienic but needs syntax parameters. You can find a full solution here: https://www.gnu.org/software/guile/manual/html_node/Syntax-Parameters.html.
>
> Note that the semantics are different. In the first solution, `return' is lexically bound; in the second solution, `return' is not rebound, but its meaning changes during the expansion of the procedure body. Moreover, in the first solution, `return' is an ordinary procedure (a continuation, to be more precise); in the second, it is a syntactic keyword.
>
> When the Scheme system has syntax parameters (like Guile), the second solution is probably the better one.

i will use the second solution,
thanks again

Damien
>
> Marc
> damien...@gmail.com schrieb am Montag, 28. Juni 2021 um 01:01:53 UTC+2:
> > hi,
> >
> > i wanted to create a macro that is used like a function definition and allow return using call/cc:
> >
> > (define-syntax def
> > (syntax-rules (return)
> > ((_ (name args ...) body body* ...)
> > (define name (lambda (args ...)
> > (call/cc (lambda (return) body body* ...)))))
> > ((_ name expr) (define name expr))))
> >
> > unfortunaly i got error:
> >
> > scheme@(guile-user)> (def (test x) (cond ((= x 3) 7) ((= x 2) (return 5)) (else 3)))
> > ;;; <stdin>:2:42: warning: possibly unbound variable `return'
> > scheme@(guile-user)> (test 2)
> > ice-9/boot-9.scm:1685:16: In procedure raise-exception:
> > Unbound variable: return
> >
> >
> > any idea?
> >
> > Damien

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor