unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Linas Vepstas <linasvepstas@gmail.com>
To: Taylan Kammer <taylan.kammer@gmail.com>
Cc: Guile User <guile-user@gnu.org>
Subject: Re: Pure (side-effect-free) calls into c/c++?
Date: Sat, 11 Jan 2020 22:12:50 -0600	[thread overview]
Message-ID: <CAHrUA35VqLODWt3Jf7-W_m3PJ9iRC1_8_d_PNMFjRKWHpB08yQ@mail.gmail.com> (raw)
In-Reply-To: <7c06af2b-3ae7-d233-8ac9-5aabb9dff2de@gmail.com>

Thank you Taylan!

So let me seize on this statement:

On Sat, Jan 11, 2020 at 3:56 PM Taylan Kammer <taylan.kammer@gmail.com>
wrote:

> On 11.01.2020 19:52, Linas Vepstas wrote:
>
> When you compile the code, all that actually ends up in the program is
> "(display ...)" with no trace of the original "(symbol-syntax? ...)"
> expression being left.  That's what macros do; they replace code at
> compile time and leave no trace of themselves.
>

OK! So .. now how do I combine that with compile-time memoization?  That
is, I want to have the memoization run in the macro itself, during macro
expansion, and have the macro evaluate into that result (i.e. the macro
expands into a runtime constant)

My first attempt fails.   I create a file with the following contents:

(use-modules (srfi srfi-1))

; The function in question. It needs to become a runtime
; constant, for constant arguments.
(define (bar x)
   (format #t "Called bar with ~A\n" x)
   (+ x 1))

; Memoization boilerplate
(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))

: First (failed) attempt at compile-time memoization
(define-syntax foob
   (syntax-rules ()
      ((foob EXP)
         (if (symbol? (quote EXP))
            (begin (display "Its a symbol\n") (bar EXP))
            (let ((junk (format #t "A hash lookup is happening for ~A\n"
EXP))
                  (const-val (hashx-ref int-hash int-assoc cache EXP)))
               (display "It's a constant!\n")
               (if (not const-val)
                  (begin
                     (set! const-val (bar EXP))
                     (hashx-set! int-hash int-assoc cache EXP const-val)))
               const-val)))))

The I compile it: (implictly)

scheme@(guile-user)> (load "/tmp/mem.scm")
;;; note: auto-compilation is enabled, set GUILE_AUTO_COMPILE=0
;;;       or pass the --no-auto-compile argument to disable.
;;; compiling /tmp/mem.scm
;;; compiled /home/ubuntu/.cache/guile/ccache/3.0-LE-8-4.1/tmp/mem.scm.go

 Hmm OK.

Lets try it out:

scheme@(guile-user)> (define x 68)
scheme@(guile-user)> (+ (foob 42) (foob x) (foob 42))
A hash lookup is happening for 42
It's a constant!
Called bar with 42
Its a symbol
Called bar with 68
A hash lookup is happening for 42
It's a constant!
$1 = 155

So, I don't mind the first four print statements. The fifth statement I
object to: I was hoping that it would not print; that the macro expansion
would cause the second occurrence of  (foob 42) to simply be expanded into
"43" (because the macro looks it up, and uses the value it found).

Now, I understand why my code doesn't work. But I can't quite figure out
how to  make it run the way I'm describing it: to force the macro to expand
the second (and subsequent) (foob 42) into the value 43.

(Side issue: how do I tell apart `42`  from `(* 33 x)` ?  The earlier
syntax-symbol? cannot tell them apart)

-- Linas

p.s. re pattern-matching: my c++ system is a giant-size pattern matcher; it
can unify multiple clauses, defining a graph, and then run the
pattern-match over huge datasets (e.g. genomic/proteomic/reactomic data)
looking for specific pathways.

 --
cassette tapes - analog TV - film cameras - you


  parent reply	other threads:[~2020-01-12  4:12 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 [this message]
2020-01-12  2:12       ` Linas Vepstas
2020-01-12  3:21         ` Taylan Kammer
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=CAHrUA35VqLODWt3Jf7-W_m3PJ9iRC1_8_d_PNMFjRKWHpB08yQ@mail.gmail.com \
    --to=linasvepstas@gmail.com \
    --cc=guile-user@gnu.org \
    --cc=taylan.kammer@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).