From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: taylanbayirli@gmail.com (Taylan Ulrich =?utf-8?Q?Bay=C4=B1rl=C4=B1?= =?utf-8?Q?=2FKammer?=) Newsgroups: gmane.lisp.guile.devel,gmane.lisp.guile.user Subject: Re: Do-loop enigma with two variables Date: Sat, 16 Jul 2016 10:55:28 +0200 Message-ID: <87r3auvu67.fsf@T420.taylan> References: <5788F2A8.8000803@gmail.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1468659358 7377 80.91.229.3 (16 Jul 2016 08:55:58 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 16 Jul 2016 08:55:58 +0000 (UTC) Cc: guile-user@gnu.org, guile-devel@gnu.org To: Pierre Lairez Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Sat Jul 16 10:55:56 2016 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1bOLO6-0006RG-FI for guile-devel@m.gmane.org; Sat, 16 Jul 2016 10:55:54 +0200 Original-Received: from localhost ([::1]:36485 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bOLO5-00018l-Oj for guile-devel@m.gmane.org; Sat, 16 Jul 2016 04:55:53 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:33538) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bOLNl-00018Z-8v for guile-devel@gnu.org; Sat, 16 Jul 2016 04:55:34 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bOLNj-0005CN-BN for guile-devel@gnu.org; Sat, 16 Jul 2016 04:55:32 -0400 Original-Received: from mail-wm0-x22b.google.com ([2a00:1450:400c:c09::22b]:35251) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bOLNj-0005C4-49; Sat, 16 Jul 2016 04:55:31 -0400 Original-Received: by mail-wm0-x22b.google.com with SMTP id f65so50193852wmi.0; Sat, 16 Jul 2016 01:55:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version:content-transfer-encoding; bh=SJPmFpblDdXr7KNxuX5H9HFYCnZFMoCVumgdnRcE7eE=; b=Qh5R58LGdwLxlnbJM/DDQ4UDMCZ5mPspvK8o51xv1TUKTtY93vSjgsDXjQmF3wAVRs 0+6LdZSgRLJanPKj1GjXHrqtOrg0yn3WYYkJYB1D94Nm3dVYEygBi8OH8/XPHzBjTNRU AF33ByQbF4YlS7pPZdFhy7JxfV08n8juJbR4lfxYNHZRRFnnV5VrxZayVDsSZgVk0mbm c8mVheVJCKuldWhkuw8UyZk8wU7fzzrQeS65gVeD7lsgXnP7z8cWUNX5kTgjN3o+4wil JfDzBc2/uVPK5TvM+vQ05F+eM5f9rRhEskQqWwwwtfCFDilrPV4Q9o7SyJuB89A8Pm7h H6Lg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version:content-transfer-encoding; bh=SJPmFpblDdXr7KNxuX5H9HFYCnZFMoCVumgdnRcE7eE=; b=L1jIhMoYunntG8IFKQb8gD0C6EkmMyn1MvP6TG7QGozWtjL90BAMuG8Y/VIwz8v7nn WY+RghezKJVwWihtuT7eQVZ6+SB75N9ISGr9dMoWRsKUZJzTG6/PnEQUErGZh864ey2x PG3VmtAY39k26BlKeqJkXcRsjWFJg0DOFqdtyJ/XmzgMslDpbK1qlbSG2SNxrLD23pn1 yt/TK67IvLfzn7SfBvOcmkxLXj+tsvc8yxSD4zQPPdrWGLcvX9m/baporZBIrRCMXapa 5dG/v377VDIIaZNscr/OLDLhMOhQ71fQ6oiOkTdyTvvqY6jTVq4kQqA+SO65KzdcnCA4 fLeA== X-Gm-Message-State: ALyK8tJcOjCwcBDmvXKkoG6uumvNhLEn5BrD3TH8StsebamnHTDN36Cmq7wFnqzs26zbew== X-Received: by 10.28.183.134 with SMTP id h128mr27344664wmf.7.1468659329854; Sat, 16 Jul 2016 01:55:29 -0700 (PDT) Original-Received: from T420.taylan ([2a02:908:c30:3540:221:ccff:fe66:68f0]) by smtp.gmail.com with ESMTPSA id e8sm4670309wjl.26.2016.07.16.01.55.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 16 Jul 2016 01:55:29 -0700 (PDT) In-Reply-To: <5788F2A8.8000803@gmail.com> (Pierre Lairez's message of "Fri, 15 Jul 2016 16:26:48 +0200") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::22b X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Original-Sender: "guile-devel" Xref: news.gmane.org gmane.lisp.guile.devel:18529 gmane.lisp.guile.user:12763 Archived-At: Pierre Lairez writes: > Dear guile users, > > When running the following loop: > (do ((i 1 (+ 1 i)) > (j 0 i)) > ((> i 4) (newline)) > (display (list i j))) > > I expect without hesitation to read > (1 0)(2 1)(3 2)(4 3) > > To my surprise, I obtain > (1 0)(2 2)(3 3)(4 4) > > After macro-expansion, the loop above is rewritten in the following: > (letrec ((loop > (=CE=BB (i j) > (if (> i 4) > (newline) > (begin > (display (list i j)) > (loop (+ 1 i) i)))))) > (loop 1 0)) > > The equality j =3D i + 1 is clearly a loop invariant, the function =E2=80= =9Cloop=E2=80=9C > is *never* called with two equal arguments. So I cannot understand why > (2 2) may possibly appear in the output. What do I not see? > > Best regards, > > Pierre I suspect this is a bug in the optimizer; guile-devel CC'd. Using 2.0.11 (gotta upgrade!): scheme@(guile-user)> ,expand (do ((i 1 (+ 1 i)) (j 0 i)) ((> i 4) (newline)) (display (list i j))) $5 =3D (let loop ((i 1) (j 0)) (if (> i 4) (begin (if #f #f) (newline)) (begin (display (list i j)) (loop (+ 1 i) i)))) ;; Looks good to me. Let's use the interpreter on it: scheme@(guile-user)> (eval '(let loop ((i 1) (j 0)) (if (> i 4) (begin (if #f #f) (newline)) (begin (display (list i j)) (loop (+ 1 i)= i)))) ((@ (rnrs eval) environment) '(guile))) (1 0)(2 1)(3 2)(4 3) ;; Looks good to me as well. Now the compiler's optimizer: scheme@(guile-user)> ,optimize (let loop ((i 1) (j 0)) (if (> i 4) (begin (if #f #f) (newline)) (begin (display (list i j)) (loop (+ 1= i) i)))) $6 =3D (begin (display (list 1 0)) (begin (display (list 2 2)) (begin (display (list 3 3)) (begin (display (list 4 4)) (newline))))) ;; Beep! (Note that the Guile REPL compiles the given expressions by default.) Taylan