Adding a Par construct to Python?

Aaron Brady castironpi at gmail.com
Wed May 20 06:41:08 CEST 2009


On May 19, 11:20 pm, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
> Steven D'Aprano <ste... at 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.



More information about the Python-list mailing list