#11776: Holding an expression unevaluated: Something like hold_all() would be 
nice.
---------------------------+------------------------------------------------
   Reporter:  Pap          |          Owner:  jason     
       Type:  enhancement  |         Status:  new       
   Priority:  minor        |      Milestone:  sage-4.7.2
  Component:  misc         |       Keywords:            
Work_issues:               |       Upstream:  N/A       
   Reviewer:               |         Author:            
     Merged:               |   Dependencies:            
---------------------------+------------------------------------------------

Comment(by nbruin):

 Replying to [comment:2 Pap]:
 > I know, that's why I suggested a different approach in the end of the
 Sage worksheet I posted (http://test.sagenb.org/home/pub/6). Something
 like
 {{{
 unevaluated_expr=hold_all( integrate(x^2+1,x) + diff(tan(x),x) )
 }}}

 That is exactly what can't work. This is equivalent to the python code
 {{{
 unevaluated_expr=hold_all(integrate(x^2+1,x).__add__(diff(tan(x),x)))
 }}}
 The meaning of this in python is that what is inside the parentheses gets
 executed and the result of that gets passed to {{{hold_all}}}. By the time
 {{{hold_all}}} executes, it is already too late. You need to inform
 {{{integrate}}}, {{{__add__}}} and {{{diff}}} that they need to behave
 differently from what they would normally do. The "hold" parameter does
 that, but as you observe, it is rather burdensome that it potentially has
 to be supplied to all routines involved (the fact that not all relevant
 routines accept "hold" yet is just a matter of a bug to fix).

 Basically what is needed is a flag on SR that sets "hold=True" to be the
 default rather than "hold=False". I don't know how easy and how thread-
 safe such a flag would be. It would definitely violate the stipulation
 that parents be immutable.

 A "clean" solution would be to allow a second instance of SR that does
 have "hold=True" as default? This would allow something that you'll
 probably find painful too:
 {{{
 sage: SRheld = SymbolicRing( hold_by_default = True )
 sage: SRheld(1) + SRheld(3) #note the need to turn 1,3 into symbolic
 objects before adding
 1 + 3
 sage: x = SRheld.var('x')
 sage: integrate(x^2+1,x)
 integral(x^2+1,x)
 sage: xt = SR(x) # the normal SR still behaves as before
 sage: integrate(xt^2+1,xt)
 1/3*x^3+x
 }}}
 I'm afraid the proposed namespace magic I proposed earlier will never
 fully work because {{{SR(1).__add__(3)}}} can't be reached that way. So we
 need to find a convenient place to store the "hold default value". SR
 itself would be a reasonable place except that changing the value
 definitely changes how SR behaves and parents are supposed to be
 immutable.

 Perhaps if we make a context manager that sets and resets a
 "hold_by_default" flag, we localize the potential trouble a bit.
 {{{localvars}}} has set a precedent for such:
 {{{
 with held_function_context(SR):
    expr=sin(pi)^2+cos(pi)^2
 }}}
 If in addition it would hold a lock on SR we would be thread safe as well
 (just not very thread friendly).

 I think there is merit in having "hold" facilities more readily available
 but to implement it requires some serious architectural considerations and
 likely some relatively comprehensive modifications.

-- 
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/11776#comment:3>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica, 
and MATLAB

-- 
You received this message because you are subscribed to the Google Groups 
"sage-trac" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sage-trac?hl=en.

Reply via email to