From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Linas Vepstas Newsgroups: gmane.lisp.guile.user Subject: Re: Pure (side-effect-free) calls into c/c++? Date: Sat, 11 Jan 2020 22:12:50 -0600 Message-ID: References: <7c06af2b-3ae7-d233-8ac9-5aabb9dff2de@gmail.com> Reply-To: linasvepstas@gmail.com Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="142121"; mail-complaints-to="usenet@blaine.gmane.org" Cc: Guile User To: Taylan Kammer Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sun Jan 12 05:15:06 2020 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1iqUcu-000KLM-4O for guile-user@m.gmane-mx.org; Sun, 12 Jan 2020 05:13:24 +0100 Original-Received: from localhost ([::1]:35046 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iqUcp-00026V-QF for guile-user@m.gmane-mx.org; Sat, 11 Jan 2020 23:13:19 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:41241) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iqUcc-0001zq-AG for guile-user@gnu.org; Sat, 11 Jan 2020 23:13:08 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iqUca-0006dp-MW for guile-user@gnu.org; Sat, 11 Jan 2020 23:13:06 -0500 Original-Received: from mail-lj1-x22f.google.com ([2a00:1450:4864:20::22f]:36846) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iqUca-0006Uf-90 for guile-user@gnu.org; Sat, 11 Jan 2020 23:13:04 -0500 Original-Received: by mail-lj1-x22f.google.com with SMTP id r19so6354341ljg.3 for ; Sat, 11 Jan 2020 20:13:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:reply-to:from:date:message-id :subject:to:cc; bh=d75Y7YBHTfXscIUWK/rmg6t6noYlgHOj90/W7EY5mY0=; b=QYYxwggt3wc0atWxITGFdp5vlnen+j3WVe3QEjOjYO7Jjh+U5qh7J4XMFud3hlPcRx Vs+/15UpiZaLUb3UjuEg4qaYMXeYdpQRwTgE1dn44D8pYZK3y8+rULdP0gYeBGY4mzxL icTQ3bcara6tIQkX5S7/dzTAuLI90opG8XUPEWHwQ11r6OHoUNldkkGzg5SEaTEjaxAA /WicNkt+74xv5jYtX8BUB9mk5j4CAYLwTrnv23ysTrM4YNhbdX1wZWH0k5962JjOCuir ez+PWBIcEa7SaGwNMLDudE/ieJADxl1PDXdaGD6tpHMJAbb+bJMiT76PFAfEZyc5D7IK xeBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:reply-to :from:date:message-id:subject:to:cc; bh=d75Y7YBHTfXscIUWK/rmg6t6noYlgHOj90/W7EY5mY0=; b=DN7j/d6VP1rDQyfhnTUnLzipB4NE+AUO8sqyUucjzFGOZQjPjbHgoCzxI/0AJ0rHG1 UZ8T1GsCQ116zJf4cFKMg4iJ3Mpsq7AnyY3jYsaAdjjidL9IjXjyiK/15j/SslxelvNT 9qrqcuaaQDqjpPrlkSS0gXgIOj8XUsNspY6AiLWuytQaWwE932JVaYWoePc4J3hW1Irc vjUczl4JmP6umw9KYgdq9Jh+rYSf60axzPgTUmRo1jMHJSQpoaQwK+gWIK9rRrtso1BL l/x5c6rOMtGeZX3DWbP60klRFz8tqhbVatf1YgC6AwVRO8ZuDrVQSH7XdZKO40McD9y5 e72A== X-Gm-Message-State: APjAAAVALMSk5nwpfH7JKDV9/B/vWzk1w+KUefdMeh5xFYVTGEx5cB0V SKzUBzUD/sKwyoD7MFq8Q+nsXENvN0ovWbQwGtY= X-Google-Smtp-Source: APXvYqy3+4EHGj47/JkfK3o70TDEUq1sCMqk+V58W99BXx2BtE8nalCIK2LgGIePgiFpNO0MJiCGy41lW6D+iUV6xv4= X-Received: by 2002:a2e:8698:: with SMTP id l24mr7152231lji.94.1578802382521; Sat, 11 Jan 2020 20:13:02 -0800 (PST) In-Reply-To: <7c06af2b-3ae7-d233-8ac9-5aabb9dff2de@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::22f X-Content-Filtered-By: Mailman/MimeDel 2.1.23 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.org gmane.lisp.guile.user:16039 Archived-At: Thank you Taylan! So let me seize on this statement: On Sat, Jan 11, 2020 at 3:56 PM Taylan Kammer 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