From: Taylan Kammer <taylan.kammer@gmail.com>
To: linasvepstas@gmail.com, Matt Wette <matt.wette@gmail.com>
Cc: Guile User <guile-user@gnu.org>
Subject: Re: Pure (side-effect-free) calls into c/c++?
Date: Sun, 12 Jan 2020 04:21:49 +0100 [thread overview]
Message-ID: <dbd9acca-3c94-a8b9-b9f2-f3a6c86106df@gmail.com> (raw)
In-Reply-To: <CAHrUA35Jy7p=VB4UZewBWn-sNgOzsKy4_6CS1khVhfawLj0oAg@mail.gmail.com>
On 12.01.2020 03:12, Linas Vepstas wrote:
> To answer my own question, it appears doable with a very simply macro, then:
>
> (define (bar x)
> (format #t "Called bar with ~A\n" x)
> (+ x 1))
>
> (define (memoize FUNC)
> "
> memoize a function FUNC which takes a single int as argument
> "
> (define cache (make-hash-table))
> (define (int-hash INT SZ) (modulo INT SZ))
> (define (int-assoc INT ILIST)
> (find (lambda (pr) (equal? INT (car pr))) ILIST))
>
> (lambda (ITEM)
> (define val (hashx-ref int-hash int-assoc cache ITEM))
> (if val val
> (let ((fv (FUNC ITEM)))
> (hashx-set! int-hash int-assoc cache ITEM fv)
> fv)))
> )
>
> (define bar-memo (memoize bar))
>
> (define-syntax foo
> (syntax-rules ()
> ((foo exp)
> (if (symbol? (quote exp))
> (begin (display "its a symb\n") (bar exp))
> (begin (display "no its not\n") (bar-memo exp))))))
Using (quote exp) in the macro output to determine if the input was a
symbol is... smart I guess! There's a problem in your approach though:
the memoization and the lookup of the memoized value will happen at
run-time, because your macro is merely emitting code that calls the
function with the memoization cache.
This also means, of course, that checking whether the argument was a
symbol is useless.
In your (lambda (ITEM) ...) definition, insert some (display ...) lines
to print "was already memoized" or "will now be memoized" and you will
see what I mean. Here's how calls to the function might then look:
scheme> (define x 66)
scheme> (bar-memo x)
will now be memoized
$1 = 67
scheme> (bar-memo x)
was already memoized
$2 = 67
scheme> (set! x 42)
scheme> (bar-memo x)
will now be memoized
$3 = 43
scheme> (bar-memo x)
was already memoized
$4 = 43
It's important to understand that Scheme uses "pass-by-value" semantics
in procedure calls. That means that a procedure doesn't know how the
values it receives were computed -- whether they were constants, or
variable references, or the result of another function call.
Consider:
(foo 42)
(let ((x 42))
(foo x))
(foo (+ 21 21))
In all three cases, foo receives the same value, 42, and doesn't know
how it arrived.
I'm not sure if memoization is really what you want, since it's a
run-time caching mechanism.
It might be possible to create a sort of "compile-time memoization" but
it would be quite complicated. For each constant value the macro
receives, it would emit a global variable definition that will be bound
to the call to the actual function at run-time, and then each call with
the same constant would emit a reference to that variable.
So the following:
(display (f-memo 42))
(display (f-memo 66))
(display (f-memo 42))
(display (f-memo 66))
would magically emit code like:
(define _x1 (f 42))
(define _x2 (f 66))
(display _x1)
(display _x2)
(display _x1)
(display _x2)
But I'm not sure how I'd write that hypothetical f-memo macro. You
can't really make a macro-call deep within some code emit a top-level
variable definition. All I can imagine are some pretty dirty methods
that probably aren't worth the complexity.
- Taylan
next prev parent reply other threads:[~2020-01-12 3:21 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-01-10 22:36 Pure (side-effect-free) calls into c/c++? Linas Vepstas
2020-01-11 14:13 ` Zelphir Kaltstahl
2020-01-11 14:38 ` Matt Wette
2020-01-11 18:11 ` Linas Vepstas
2020-01-11 18:52 ` Linas Vepstas
2020-01-11 21:56 ` Taylan Kammer
2020-01-12 2:15 ` Linas Vepstas
2020-01-12 4:12 ` Linas Vepstas
2020-01-12 2:12 ` Linas Vepstas
2020-01-12 3:21 ` Taylan Kammer [this message]
2020-01-12 4:32 ` Linas Vepstas
2020-01-12 5:52 ` Linas Vepstas
2020-01-11 17:40 ` Linus Björnstam
2020-01-12 3:03 ` Christopher Lam
2020-01-12 10:35 ` Linus Björnstam
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=dbd9acca-3c94-a8b9-b9f2-f3a6c86106df@gmail.com \
--to=taylan.kammer@gmail.com \
--cc=guile-user@gnu.org \
--cc=linasvepstas@gmail.com \
--cc=matt.wette@gmail.com \
/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).