Scope, assumptions, asksign, context. Was Re: Bug in Maxima 5.30.0
Subject: Scope, assumptions, asksign, context. Was Re: Bug in Maxima 5.30.0
From: Stavros Macrakis
Date: Thu, 5 Sep 2013 18:52:02 -0400
On Thu, Sep 5, 2013 at 1:50 PM, Robert Dodier <robert.dodier at gmail.com>wrote:
> On 2013-09-04, Stavros Macrakis <macrakis at alum.mit.edu> wrote:
>
> > Quick partial answer: take a look at the R functions environment,
> > parent.env, parent.frame, ....
>
> > For an example of a difficult-to-solve problem, see this email
> > chain<https://groups.google.com/forum/#!topic/manipulatr/Jr7S_1GX5Yk>
> > where I reported a bug in the plyr package.
>
> Well, the problem that's reported there is pretty specific to R, right?
>
> because of the frame business -- Maxima doesn't have anything like that.
>
You asked for an example of explicit environment manipulation in R. It is
relevant to the Maxima issues we're discussing because it addresses the
problem of evaluating an expression (as opposed to a function closure) in
an environment constructed by the defined function. It is of course not *
directly* transposable to Maxima because Maxima and R have somewhat
different semantics. R in particular, in addition to closures, also has
environments as data objects and idiosyncratic argument evaluation
semantics ("promises"). Lisp and Maxima do have environments and frames,
they just don't let you manipulate them explicitly the way R does....
> To give us something to talk about, here is a quick n dirty
> implementation of with_posval. I don't pretend this is anywhere near a
> finished product.
>
Hmm, I guess I didn't make it clear what my point was with the with_posval
example. It is certainly possible to write with_posval in Lisp without
danger of variable collisions, and using supcontext and unwind-protect for
contexts. It's probably even possible to write it in Maxima with some
acrobatics around variable naming. (And I don't consider writing it as a
macro to be a reasonable solution.)
However, I would claim that our current primitives make it difficult to
write it in Maxima in a clear, simple way.
-s
> best
>
> Robert Dodier
>
> PS.
> $ cat with_posval.lisp
> ;; copyright 2013 by Robert Dodier
> ;; I release this work under terms of the GNU General Public License
>
> (defmspec $with_posval (foo)
> (let
> ((expr (second foo))
> (vars (meval (third foo)))
> (ctxt-name (gensym)))
> (unwind-protect
> (progn
> (mfuncall '$supcontext ctxt-name)
> (assume-positive (rest vars))
> (meval expr))
> (mfuncall '$killcontext ctxt-name))))
>
> (defun assume-positive (vars)
> (mapcar #'(lambda (x) (mfuncall '$assume `((mgreaterp) ,x 0))) vars))
>
>
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
>