From debbugs-submit-bounces@debbugs.gnu.org Thu Jan 16 07:46:55 2014 Received: (at submit) by debbugs.gnu.org; 16 Jan 2014 12:46:55 +0000 Received: from localhost ([127.0.0.1]:52965 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1W3mLW-0005gp-2M for submit@debbugs.gnu.org; Thu, 16 Jan 2014 07:46:54 -0500 Received: from eggs.gnu.org ([208.118.235.92]:33710) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1W3mLS-0005gg-JY for submit@debbugs.gnu.org; Thu, 16 Jan 2014 07:46:51 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1W3mLQ-0001Jh-Iw for submit@debbugs.gnu.org; Thu, 16 Jan 2014 07:46:49 -0500 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on eggs.gnu.org X-Spam-Level: X-Spam-Status: No, score=0.8 required=5.0 tests=BAYES_50 autolearn=disabled version=3.3.2 Received: from lists.gnu.org ([2001:4830:134:3::11]:51151) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W3mLQ-0001Jd-G5 for submit@debbugs.gnu.org; Thu, 16 Jan 2014 07:46:48 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35430) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W3mLP-0005TV-9V for bug-guile@gnu.org; Thu, 16 Jan 2014 07:46:48 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1W3mLJ-0001Iu-BD for bug-guile@gnu.org; Thu, 16 Jan 2014 07:46:47 -0500 Received: from river.fysh.org ([2001:41d0:8:d47f::2]:38169) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W3mLJ-0001IW-4m for bug-guile@gnu.org; Thu, 16 Jan 2014 07:46:41 -0500 Received: from zefram by river.fysh.org with local (Exim 4.80 #2 (Debian)) id 1W3mL7-0003x3-IG; Thu, 16 Jan 2014 12:46:29 +0000 Date: Thu, 16 Jan 2014 12:46:29 +0000 From: Zefram To: bug-guile@gnu.org Subject: + folding differs between compiler and interpreter Message-ID: <20140116124629.GC21945@fysh.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2001:4830:134:3::11 X-Spam-Score: -4.0 (----) X-Debbugs-Envelope-To: submit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -4.0 (----) The + procedure left-folds its arguments in interpreted code and right-folds its arguments in compiled code. This may or may not be a bug. Obviously, with exact numbers the direction of folding makes no difference. But the difference is easily seen with flonums, as flonum addition is necessarily non-associative. For example, where flonums are IEEE doubles: scheme@(guile-user)> ,o interp #f scheme@(guile-user)> (+ 1.0 (expt 2.0 -53) (expt 2.0 -53)) $1 = 1.0000000000000002 scheme@(guile-user)> (+ (expt 2.0 -53) (expt 2.0 -53) 1.0) $2 = 1.0 scheme@(guile-user)> ,o interp #t scheme@(guile-user)> (+ 1.0 (expt 2.0 -53) (expt 2.0 -53)) $3 = 1.0 scheme@(guile-user)> (+ (expt 2.0 -53) (expt 2.0 -53) 1.0) $4 = 1.0000000000000002 Compiler and interpreter agree when the order of operations is explicitly specified: scheme@(guile-user)> (+ (+ 1.0 (expt 2.0 -53)) (expt 2.0 -53)) $5 = 1.0 scheme@(guile-user)> (+ 1.0 (+ (expt 2.0 -53) (expt 2.0 -53))) $6 = 1.0000000000000002 If your flonums are not IEEE double then the exponent in the test case has to be adapted. R5RS and the Guile documentation are both silent about the order of operations in cases like this. I do not regard either left-folding or right-folding per se as a bug. A portable Scheme program obviously can't rely on a particular behaviour. My concern here is that the compiler and interpreter don't match, making program behaviour inconsistent on what is notionally a single implementation. That mismatch may be a bug. I'm not aware of any statement either way on whether you regard such mismatches as bugs. (An explicit statement in the documentation would be most welcome.) R6RS does have some guidance about the proper behaviour here. The description of the generic arithmetic operators doesn't go into such detail, just describing it as generic. It can be read as implying that the behaviour on flonums should match the behaviour of the flonum-specific fl+. The description of fl+ (libraries section 11.3 "Flonums") says it "should return the flonum that best approximates the mathematical sum". That suggests that it shouldn't use a fixed sequence of dyadic additions operations, and in my test case should return 1.0000000000000002 regardless of the order of operands. Obviously that's more difficult to achieve than just folding the argument list with dyadic addition. Interestingly, fl+'s actual behaviour differs both from + and from the R6RS ideal. It left-folds in both compiled and interpreted code: scheme@(guile-user)> (import (rnrs arithmetic flonums (6))) scheme@(guile-user)> ,o interp #f scheme@(guile-user)> (fl+ 1.0 (expt 2.0 -53) (expt 2.0 -53)) $7 = 1.0 scheme@(guile-user)> (fl+ (expt 2.0 -53) (expt 2.0 -53) 1.0) $8 = 1.0000000000000002 scheme@(guile-user)> ,o interp #t scheme@(guile-user)> (fl+ 1.0 (expt 2.0 -53) (expt 2.0 -53)) $9 = 1.0 scheme@(guile-user)> (fl+ (expt 2.0 -53) (expt 2.0 -53) 1.0) $10 = 1.0000000000000002 fl+'s behaviour is not a bug. The R6RS ideal is clearly not mandatory, and the Guile documentation makes no stronger claim than that its fl+ conforms to R6RS. As it is consistent between compiler and interpreter, it is not subject to the concern that I'm raising in this ticket about the generic +. -zefram From debbugs-submit-bounces@debbugs.gnu.org Thu Jan 16 08:30:59 2014 Received: (at 16464-close) by debbugs.gnu.org; 16 Jan 2014 13:30:59 +0000 Received: from localhost ([127.0.0.1]:52992 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1W3n2A-0006sF-K6 for submit@debbugs.gnu.org; Thu, 16 Jan 2014 08:30:59 -0500 Received: from world.peace.net ([96.39.62.75]:50814) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1W3n26-0006rz-LN; Thu, 16 Jan 2014 08:30:55 -0500 Received: from 209-6-91-212.c3-0.smr-ubr1.sbo-smr.ma.cable.rcn.com ([209.6.91.212] helo=yeeloong) by world.peace.net with esmtpsa (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.72) (envelope-from ) id 1W3n1y-0007RU-9o; Thu, 16 Jan 2014 08:30:46 -0500 From: Mark H Weaver To: Zefram Subject: Re: bug#16464: + folding differs between compiler and interpreter References: <20140116124629.GC21945@fysh.org> Date: Thu, 16 Jan 2014 08:28:28 -0500 In-Reply-To: <20140116124629.GC21945@fysh.org> (zefram@fysh.org's message of "Thu, 16 Jan 2014 12:46:29 +0000") Message-ID: <87lhygqcn7.fsf@netris.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 16464-close Cc: 16464-close@debbugs.gnu.org, request@debbugs.gnu.org X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: 0.0 (/) tags 16464 notabug thanks Zefram writes: > The + procedure left-folds its arguments in interpreted code and > right-folds its arguments in compiled code. This may or may not be a > bug. [...] > R5RS and the Guile documentation are both silent about the order of > operations in cases like this. I do not regard either left-folding or > right-folding per se as a bug. A portable Scheme program obviously can't > rely on a particular behaviour. My concern here is that the compiler > and interpreter don't match, making program behaviour inconsistent on > what is notionally a single implementation. That mismatch may be a bug. As you suggest, this is not a bug. The compiler is free to apply a different ordering to each '+' and '*' within a program, which can be used to good effect by an optimizer. > Obviously, with exact numbers the direction of folding makes no > difference. But the difference is easily seen with flonums, as flonum > addition is necessarily non-associative. Indeed, and if you need to explicitly order the operations (and this _is_ often needed when working with inexacts to prevent things like catastrophic annihilation), then you can do so using extra parentheses. BTW, there are _many_ other cases like this, where certain aspects behavior are left unspecified, and might not only differ between the compiler and interpreter, but might even differ between two identical subexpressions. Most notably, the order in which the operand and operands of a procedure call are evaluated is left unspecified, and again, this can be used to good effect in a compiler for optimization purposes. Another is that the initializers in a 'let' or 'letrec' (but not 'letrec*') can be evaluated in any order. However, there _are_ some related bugs that you missed. R5RS specifies that '-' and '/' are left-associative, which implies that that (- x y z) must evaluate as (- (- x y) z), and similiarly for '/', but in 2.0.9 (and afaik, in all existing 2.0.x releases), we transform (- x y z) to (- x (+ y z)), which is incorrect. I discovered and fixed these bugs in August in the git repository, and they will be part of Guile 2.0.10. I also changed the order of operations of '+' and '*' to be left-associative while I was at it, but I later realized that this is not required, and we reserve the right to change the order in the future. http://git.savannah.gnu.org/gitweb/?p=guile.git;a=commitdiff;h=71673fba930d735c09184d5ca115882239edabb3 I'm closing this bug now. Thanks, Mark From unknown Sat Jun 21 03:21:00 2025 Received: (at fakecontrol) by fakecontrolmessage; To: internal_control@debbugs.gnu.org From: Debbugs Internal Request Subject: Internal Control Message-Id: bug archived. Date: Fri, 14 Feb 2014 12:24:05 +0000 User-Agent: Fakemail v42.6.9 # This is a fake control message. # # The action: # bug archived. thanks # This fakemail brought to you by your local debbugs # administrator