PATCH: Translating maxima macros



Hi.

This naive patch of course does not work in general. Maybe I should
explain the problem. I have a file with lots of functions, some of
which benefit from compiling. So after loading the file I want to do
compile(all). This breaks some functions which use macros.

I'm using the with_parameters macro from augmented_lagrangian;

with_parameters([L])::=buildq([a:subst(:,=,ev(L[1])),e:rest(L)],block(a,splice(e)))

Define a simple function f.

(%i12) f(x, [opt]) := with_parameters(opt, if symbolp(a) then x else 1);
(%o12) f(x,[opt]):=with_parameters(opt,if symbolp(a) then x else 1)

Before compiling:

(%i13) f(x, a=1);
(%o13) 1
(%i14) f(x);
(%o14) x

(%i15) compile(f);
(%o15) [f]

After compiling

(%i16) f(x, a=1);
(%o16) x
(%i17) f(x);
(%o17) x

I want to modify the translator so that f gives the same result before
and after compiling. The only way I can think of is to disable the
translation of with_parameters form and to expand it when f is called.
This gives the results I expect but of course does not benefit from
translation. What is the correct way to compile functions which
include macros like with_parameters?

BTW: the translation does not work correctly for macros.

(%i17) translate(with_parameters);
; in: LAMBDA NIL
;     #'(LAMBDA MAXIMA::|mlexpr NARGS|
;         (COND
;          ((< MAXIMA::|mlexpr NARGS| 0)
;           (MAXIMA::$ERROR 'MAXIMA::MAXIMA-ERROR 'MAXIMA::$WITH_PARAMETERS
;                           " takes no less than " 0 '" arguments."))
;          (T ((LAMBDA # # #) (CONS '# #)))))
;
; caught ERROR:
;   The lambda expression has a missing or non-list lambda list:
;     (LAMBDA |mlexpr NARGS|
;       (COND
;        ((< |mlexpr NARGS| 0)
;         ($ERROR 'MAXIMA-ERROR '$WITH_PARAMETERS " takes no less than " 0
;                 '" arguments."))
;        (T
;         ((LAMBDA (|$l|) (DECLARE #) (MBUILDQ-SUBST # '#))
;          (CONS '(MLIST) (LISTIFY #))))))
;
; compilation unit finished
;   caught 1 ERROR condition
(%o17) []

Andrej



On Tue, Dec 1, 2009 at 11:06 AM, Andrej Vodopivec
<andrej.vodopivec at gmail.com> wrote:
> Hi,
>
> The maxima translator currently expands maxima macros at compile time
> not at run time. I think this should be corrected. Here is a patch to
> expand macros at run time.
>
> Andrej
>
> Index: transl.lisp
> ===================================================================
> RCS file: /cvsroot/maxima/maxima/src/transl.lisp,v
> retrieving revision 1.42
> diff -u -r1.42 transl.lisp
> --- transl.lisp 1 Dec 2009 07:31:34 -0000 ? ? ? 1.42
> +++ transl.lisp 1 Dec 2009 10:03:15 -0000
> @@ -748,15 +748,16 @@
> ?(defun toplevel-optimize-1 (form &aux (op (car form)) prop)
> ? (cond ((or (atom op)
> ? ? ? ? ? ? (member 'array op :test #'eq)) form)
> - ? ? ? ((progn (setq op (car op))
> - ? ? ? ? ? ? ? (setq prop
> - ? ? ? ? ? ? ? ? ? ? (if $transrun ? ? ; crock a minute.
> - ? ? ? ? ? ? ? ? ? ? ? ? (or (get op 'translated-mmacro)
> - ? ? ? ? ? ? ? ? ? ? ? ? ? ? (mget op 'mmacro))
> - ? ? ? ? ? ? ? ? ? ? ? ? (or (mget op 'mmacro)
> - ? ? ? ? ? ? ? ? ? ? ? ? ? ? (get op 'translated-mmacro)))))
> - ? ? ? ?(mmacro-apply prop form))
> - ? ? ? ((setq prop ($get op '$optimize))
> + ? ? ? ;; DONT EXPAND MACROS AT TRANSLATION TIME
> + ? ? ? ;((progn (setq op (car op)) nil))
> + ? ? ? ; ? ? ? (setq prop
> + ? ? ? ; ? ? ? ? ? ? (if $transrun ? ? ; crock a minute.
> + ? ? ? ; ? ? ? ? ? ? ? ? (or (get op 'translated-mmacro)
> + ? ? ? ; ? ? ? ? ? ? ? ? ? ? (mget op 'mmacro))
> + ? ? ? ; ? ? ? ? ? ? ? ? (or (mget op 'mmacro)
> + ? ? ? ; ? ? ? ? ? ? ? ? ? ? (get op 'translated-mmacro)))))
> + ? ? ? ; (mmacro-apply prop form))
> + ? ? ? ((progn (setq op (car op)) (setq prop ($get op '$optimize)))
> ? ? ? ? ;; interesting, the MAPPLY here causes the simplification
> ? ? ? ? ;; of the form and the result.
> ? ? ? ? ;; The optimize property can be used to implement
> Index: fcall.lisp
> ===================================================================
> RCS file: /cvsroot/maxima/maxima/src/fcall.lisp,v
> retrieving revision 1.14
> diff -u -r1.14 fcall.lisp
> --- fcall.lisp ?1 Dec 2009 07:34:34 -0000 ? ? ? 1.14
> +++ fcall.lisp ?1 Dec 2009 10:03:15 -0000
> @@ -82,6 +82,9 @@
> ? ? ? ?((macro-function f)
> ? ? ? ? (eval (cons f list-argl)))
> ? ? ? ?((not (symbolp f)) (merror (intl:gettext "apply: expected symbol or
> function; found: ~M") f))
> + ? ? ? ;;; EXPAND MAXIMA MACROS
> + ? ? ? ((setq f-prop (or (get f 'translated-macro) (mget f 'mmacro)))
> + ? ? ? ?(meval (mmacro-apply f-prop `((,f) , at argl))))
> ? ? ? ?((setq f-prop (get f 'mfexpr*))
> ? ? ? ? (funcall f-prop (cons nil argl)))
> ? ? ? ?((setq f-prop (mget f 'mexpr))
>