Thank you Taylan! So let me seize on this statement:
On Sat, Jan 11, 2020 at 3:56 PM Taylan Kammer <taylan.kam...@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