gcl-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gcl-devel] dotimes in 2.6.2


From: Mark Saaltink
Subject: [Gcl-devel] dotimes in 2.6.2
Date: Wed, 30 Jun 2004 10:33:06 -0400

Dear GCL team,

There is an error in the DOTIMES macro in 2.6.2, as can be seen in
this example:

GCL (GNU Common Lisp)  2.6.2 CLtL1   Jun 28 2004 11:02:36

>(defconstant two 2)

TWO

>(defun foo () (dotimes (x two) (print x)))

FOO

>(compile 'foo)

Compiling gazonk0.lsp.
; (DEFUN FOO ...) is being compiled.
;;; The constant TWO is being bound.
No FASL generated.

Error: Cannot open the file NIL..


The macroexpansion makes the problem clear: TWO gets rebound.  This is
a bad idea according to both CLtL and the ANSI spec, if I read them
correctly.

>>(macroexpand-1 '(dotimes (x two) (print x)))

(COND
  ((< TWO 0) (LET ((X 0)) (DECLARE (FIXNUM X) (IGNORABLE X)) NIL))
  ((<= TWO MOST-POSITIVE-FIXNUM)
   (LET ((TWO TWO))                ; <===
     (DECLARE (FIXNUM TWO))
     (DO* ((X 0 (1+ X))) ((>= X TWO) NIL)
       (DECLARE (FIXNUM X))
       (PRINT X))))
  (T (DO* ((X 0 (1+ X))) ((>= X TWO) NIL) (PRINT X))))


Another unexpected outcome arises because of that binding:

>(defvar x2 2)

X2

>(defun bar () (dotimes (x x2) (incf x2)))

BAR

>(compile 'bar)

#<compiled-function BAR>

>(bar)

This goes into an infinite loop, which I interrupted:

Error: Console interrupt.
Fast links are on: do (si::use-fast-links nil) for debugging
Error signalled by BAR.
Broken at SYSTEM:TERMINAL-INTERRUPT.  Type :H for Help.

>>x2

118486967

The count-form is only supposed to be evaluated once, and the body executed
that number of times, so this behaviour is something of a surprise.

Perhaps a gensym should be used for the bound variable in the macro
expansion, so that these problems are avoided.

Mark




reply via email to

[Prev in Thread] Current Thread [Next in Thread]