From: Mark H Weaver <mhw@netris.org>
To: guile-user@gnu.org
Subject: Re: Self-evaluating function and closure
Date: Sun, 16 Jun 2019 07:29:59 -0400 [thread overview]
Message-ID: <878su13gz1.fsf@netris.org> (raw)
In-Reply-To: <20190612202929.GA20126@newvzh.lokolhoz> (Vladimir Zhbanov's message of "Wed, 12 Jun 2019 23:29:29 +0300")
Hello again,
Vladimir Zhbanov <vzhbanov@gmail.com> writes:
> scheme@(guile-user)> (define (function-generator)
> (let ((func #f))
> (lambda () (set! func (let a () a)) func)))
[...]
> - Is there a way to work around this (either using the above 'let'
> construct or anything else)?
Ideally, the code would be reworked to not expect equivalent procedures
to be distinguishable. However, I should probably offer a hacky but
expedient workaround. Here's one way to make otherwise equivalent
procedures distinguishable:
Allocate a fresh tag using (list #f), and arrange for the procedure to
return that tag if it's called with a special input that's outside of
the normal domain. Note that for Scheme procedures, the "input" is in
general a list of arguments of arbitrary length. You could use
'case-lambda', which creates procedures that evaluate different body
expressions depending on how many arguments are passed to it. Just add
a case for an arity that you will never use, which returns the unique
tag.
In the example you gave, (let a () a) is equivalent to:
((letrec ((a (lambda () a)))
a))
The procedure returned by (let a () a) expects 0 arguments. It will
raise an error otherwise. We can repurpose the previously erroneous
arity-1 case to return the unique tag, as follows:
(let ((unique-tag (list #f)))
((letrec ((a (case-lambda
(() a)
((x) unique-tag))))
a)))
Every time the above expression is evaluated, it will necessarily return
a unique procedure, which, if passed 0 arguments, behaves the same as
the procedure returned by (let a () a).
Mark
next prev parent reply other threads:[~2019-06-16 11:29 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-06-12 20:29 Self-evaluating function and closure Vladimir Zhbanov
2019-06-15 19:35 ` John Cowan
2019-06-16 0:39 ` Mark H Weaver
2019-06-16 0:36 ` Mark H Weaver
2019-06-16 9:02 ` Vladimir Zhbanov
2019-06-16 10:09 ` Mark H Weaver
2019-06-17 8:03 ` Vladimir Zhbanov
2019-06-16 9:47 ` Mark H Weaver
2019-06-16 10:21 ` Thomas Morley
2019-06-16 10:32 ` Mark H Weaver
2019-06-16 10:42 ` Thomas Morley
2019-06-16 11:29 ` Mark H Weaver [this message]
2019-06-17 8:48 ` Vladimir Zhbanov
2019-06-17 14:21 ` Mark H Weaver
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=878su13gz1.fsf@netris.org \
--to=mhw@netris.org \
--cc=guile-user@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).