Why won't this compile?



On 8/13/09, Richard Fateman <fateman at cs.berkeley.edu> wrote:

> 1. probably there is an error in compiling "sum". This is not
> surprising, since sum evaluates its first argument in an irregular way.

True, but actually it is due to an interaction with the translation
of the for loop ... the loop variable is lexical in translated code,
so its value can't be seen when the summand is eventually
evaluated. All Maxima variables should be special, right?
I've attached a patch to make the loop variable special in
translated code. After applying the patch I get the same
result (no error) in interpreted & compiled code.

> 2. there is probably no advantage to compiling this function or any
> other unless you use modedeclare
> to declare the types of some or all of the items you use in arithmetic.

In this case, my guess is that most of the time is spent
evaluating the sum, in already-compiled code.
I suspect compile(fun) won't help much even w/ modedeclare,
but anyway it should work the same if compiled.

Robert Dodier

PS. here's the patch:

Index: src/transl.lisp
===================================================================
RCS file: /cvsroot/maxima/maxima/src/transl.lisp,v
retrieving revision 1.40
diff -u -r1.40 transl.lisp
--- src/transl.lisp     3 Jul 2009 16:59:22 -0000       1.40
+++ src/transl.lisp     14 Aug 2009 05:11:10 -0000
@@ -1411,10 +1411,10 @@
       (setq var (tunbind (cond ((cadr form)) (t 'mdo))))
       `(,mode do ((,var ,(cdr init) ,(cdr next))
                  ,@ init-end-var )
-             (,test '$done) .
+             (,test '$done) . ((declare (special ,var)) .
              ,(cond ((atom (cdr action)) nil)
                     ((eq 'progn (cadr action)) (cddr action))
-                    (t (list (cdr action))))))))
+                    (t (list (cdr action)))))))))

 (setq shit nil)

@@ -1438,10 +1438,10 @@
        (return
         `(,mode do ((,var) (mdo (cdr ,init) (cdr mdo)))
                 ((null mdo) '$done)
-                (setq ,var (car mdo)) .
+                (setq ,var (car mdo)) . ((declare (special ,var)) .
                 ,(cond ((atom (cdr action)) nil)
                        ((eq 'progn (cadr action)) (cddr action))
-                       (t (list (cdr action)))))))))
+                       (t (list (cdr action))))))))))


 (defun lambda-wrap1 (tn val form)