[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Pure (side-effect-free) calls into c/c++?

From: Taylan Kammer
Subject: Re: Pure (side-effect-free) calls into c/c++?
Date: Sun, 12 Jan 2020 04:21:49 +0100
User-agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.3.1

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.


    (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

reply via email to

[Prev in Thread] Current Thread [Next in Thread]