On May 19, 11:20 pm, Paul Rubin <http://phr...@nospam.invalid> wrote: > Steven D'Aprano <ste...@remove.this.cybersource.com.au> writes: > > (4) the caller is responsible for making sure he never shares data while > > looping over it. > > > I don't think I've missed any possibilities. You have to pick one of > > those four. > > I wonder if the compiler can check that a given function doesn't > change any data. Then: > > @pure > def f(x): > return x*sqrt(x) + 3 # does not mutate any data > > @pure > def g(x): ... # likewise > > s = parallel_dot_product(parallel_map(f, vec), parallel_map(g,vec))
You can do the basics of this using the 'ast' module. Just check that no nodes in the ast tree are Assign nodes, including augmented assign. Then 'f' is defined as: f= pure( ''' return x*sqrt(x) + 3 # does not mutate any data ''' ) Untested. However, you do need to check that the subchildren don't make mutations. This adds hooks to the AST, since names can change at run-time. Also, it's only defined for functions that call functions that are all pure Python and defined using 'pure' as well, without inspecting the bytecode. At this point one might look into metaprogramming and script autogeneration. The 'pure' wrapper doesn't fundamentally change the semantics of the function; it is more like a pre- and post-condition check. -- http://mail.python.org/mailman/listinfo/python-list