From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: =?UTF-8?Q?Linus_Bj=C3=B6rnstam?= Newsgroups: gmane.lisp.guile.user Subject: Re: Pure (side-effect-free) calls into c/c++? Date: Sun, 12 Jan 2020 11:35:30 +0100 Message-ID: References: <365fc4d0-8c2e-4578-b38f-05ebf6c20064@www.fastmail.com> Mime-Version: 1.0 Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="38029"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Cyrus-JMAP/3.1.7-740-g7d9d84e-fmstable-20200109v1 Cc: guile-user To: "Christopher Lam" Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sun Jan 12 11:37:50 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 1iqabo-0011zS-RU for guile-user@m.gmane-mx.org; Sun, 12 Jan 2020 11:36:41 +0100 Original-Received: from localhost ([::1]:36924 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iqabn-000236-Cg for guile-user@m.gmane-mx.org; Sun, 12 Jan 2020 05:36:39 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:51935) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iqabR-00022t-Kt for guile-user@gnu.org; Sun, 12 Jan 2020 05:36:19 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iqabP-0001kR-Un for guile-user@gnu.org; Sun, 12 Jan 2020 05:36:17 -0500 Original-Received: from wout2-smtp.messagingengine.com ([64.147.123.25]:43601) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iqabP-0001dq-C9 for guile-user@gnu.org; Sun, 12 Jan 2020 05:36:15 -0500 Original-Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.west.internal (Postfix) with ESMTP id 9DCF842E; Sun, 12 Jan 2020 05:36:10 -0500 (EST) Original-Received: from imap1 ([10.202.2.51]) by compute1.internal (MEProxy); Sun, 12 Jan 2020 05:36:10 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fastmail.se; h= mime-version:message-id:in-reply-to:references:date:from:to:cc :subject:content-type:content-transfer-encoding; s=fm2; bh=dqe5G p6FTm6wticyQ/MbGb/yQv2Xyu1DXRW+BVYS2AI=; b=yLwiUd3YtY8cx14DWmsBS WiIEjaYsPeJYjigKB8s4uKYbBkLi0Ik62qEzTr2Hi35LjT58iw5rNfnMClF8ovym tr1wrCwKHP7bpDiBzHOcRdGFUeFsHF46YtA9qPbHwBFoIlMXPteSOKsLKGfRkRcO 5SdL3wr1sbz3GeoI8qRj0TZ1RHPnoB7A1Pw3qIp0uvubCE3SEkAxNhA9lastRyiZ 1fg9vEhDUjUSN9EMGZcDdGofzNdHNTLg3HeiYJJ4rDkMO48XysXjHg2sj28reY9m jLUojxdWszS2LCMqhnb+zm75xz0ZRm4AJCocrm8T8el/sRT0Ma3tICTcvnhsPKTz A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=dqe5Gp6FTm6wticyQ/MbGb/yQv2Xyu1DXRW+BVYS2 AI=; b=GMXZ69nBnopLgeuhKHi9gLOV8eqkAnQTM9z3kvV6fpM/la2FN85lEK2jV b9VcSJb4EoeASwJI67FNAB9JADiBR4vYUolK09LcwEhU2gy1/gtb0jejphHDjTNt d+EAV5hGhMryOdHzeOCex6rRjiSTMo5Au1h1wXV0NPbZiUuq5l4T/GZCs7TbSfCU nCQBu97ostY15qWX8qb5nHqGv5pCWQxvKA16aHJ4dpUxgxWkTLAH+Npe0jbhbvNs XKMNCjCZmSeUkgFc0J2tHa4XUxF/w6uuNbtmM9Z5CbED5YmV3oeRtktd2vA1iBa4 6f3qod8AePYQYDvE0c5MyH0rx3Fqg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrvdeikedgudeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefofgggkfgjfhffhffvufgtgfesth hqredtreerjeenucfhrhhomhepnfhinhhushgpuehjnphrnhhsthgrmhcuoehlihhnuhhs rdhinhhtvghrnhgvthesfhgrshhtmhgrihhlrdhsvgeqnecuffhomhgrihhnpehsrhdrhh htnecurfgrrhgrmhepmhgrihhlfhhrohhmpehlihhnuhhsrdhinhhtvghrnhgvthesfhgr shhtmhgrihhlrdhsvgenucevlhhushhtvghrufhiiigvpedt X-ME-Proxy: Original-Received: by mailuser.nyi.internal (Postfix, from userid 501) id E957CC200A4; Sun, 12 Jan 2020 05:36:09 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface In-Reply-To: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 64.147.123.25 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:16042 Archived-At: Thanks! I haven't used this macro in a billion years and wrote it as a comfort t= hingie for the first 100 project Euler problems.=20 This thread got me to start thinking about how to memoize "smarter" and = having a macro that allows you to trade a bit of speed for being able to= specifically memoize the last n or the most common n arguments. --=20 Linus Bj=C3=B6rnstam On Sun, 12 Jan 2020, at 04:03, Christopher Lam wrote: > I can add a contribution! The good thing about memoize is it's simple=20= > to create. You forgot a catch however: if the memoized return-val is #= f=20 > then your memoizer=20 > https://hg.sr.ht/~bjoli/misc/browse/default/memoize.scm will not=20 > recognise that #f is a valid cached return-val and will call the lambd= a=20 > again. (FWIW I shudder think what a *fast* memoizer would do). >=20 > Here's how I did mine: >=20 > (define (memoize f) > (let ((h (make-hash-table))) > (lambda args > (cond > ((hash-ref h args) =3D> car) > (else (let ((res (apply f args))) > (hash-set! h args (list res)) > res)))))) >=20 > (define-syntax-rule (lambda/macro args body ...) > (memoize (lambda args body ...))) >=20 > (define-syntax-rule (define/macro (f . args) body ...) > (define f lambda/macro args body ...)) >=20 >=20 >=20 > On Sat, 11 Jan 2020 at 17:40, Linus Bj=C3=B6rnstam=20 > wrote: > > I have a macro called lambda/memo and define/memo for these situatio= ns: https://hg.sr.ht/~bjoli/misc/browse/default/memoize.scm > >=20 > > If the function gets called with a gazillion different arguments th= e memoizatiin hash gets large, and there are no mechanisms to stop that = from happening. It also lacks a fast path for single argument functions.= > >=20 > > You can disregard the repo license. Use that function is you like, = if you like to. > >=20 > >=20 > > --=20 > > Linus Bj=C3=B6rnstam > >=20 > > On Fri, 10 Jan 2020, at 23:36, Linas Vepstas wrote: > > > So, I've got lots of C code wrapped up in guile, and I'd like to = declare > > > many of these functions to be pure functions, side-effect-free, t= hus > > > hopefully garnering some optimizations. Is this possible? How wou= ld I do > > > it? A cursory google-search reveals no clues. > > >=20 > > > To recap, I've got functions f and g that call into c++, but are = pure (i.e. > > > always return the same value for the same arguments). I've got > > > user-written code that looks like this: > > >=20 > > > (define (foo x) > > > (g (f 42) (f x) (f 43)) > > >=20 > > > and from what I can tell, `f` is getting called three times whene= ver the > > > user calls `foo`. I could tell the user to re-write their code to= cache, > > > manually: viz: > > >=20 > > > (define c42 (f 42)) > > > (define c43 (f 43)) > > > (define (foo x) (g c42 (f x) c43)) > > >=20 > > > but asking the users to do this is .. cumbersome. And barely wort= h it: `f` > > > takes under maybe 10 microseconds to run; so most simple-minded c= aching > > > stunts don't pay off. But since `foo` is called millions/billions= of times, > > > I'm motivated to find something spiffy. > > >=20 > > > Ideas? suggestions? > > >=20 > > > -- Linas > > > --=20 > > > cassette tapes - analog TV - film cameras - you > > > > >