Anonymus functions revisited : tuple actions

Ron_Adam radam2 at tampabay.rr.com
Thu Mar 24 18:45:26 EST 2005


On 24 Mar 2005 01:58:48 -0800, "Kay Schluehr" <kay.schluehr at gmx.net>
wrote:

>I personally don't like using exec and eval for stuff different from
>evaluating user input.

I lean the other way. I never want to use user impute for eval and
exec.  Way too risky.  But limited use, that is not user input, should
be ok. The programmer can do anything he/she wants anyways, so exec or
eval is just another tool in that respect.  

But I am trying to find a way to limit the code objects. To make it
safe I need to hide all the name spaces except locals within the
function.  Not sure if it's possible.. um, not easy. I know it's
possible.

>You rely much on "evaluate statement on the line" by adapting
>conventional Python syntax. I think one can go a bit further breaking
>the syntactical prejudices and apply tuple-actions :)
>
>Playing a bit with tuple-actions shows that the concept is quite
>powerfull and can be used to create simple statements.
>
>First of all the semantics has to be patched:
>
>We have
>
>   (x,y,z=0) -> (x,y,z)
>
>as a tuple assignment
>
>   ((x,y,z=0)->(x,y,z))(a,b,c) = (x=a,y=b,z=c)
>
>But it is not clear what
>
>   (x,y,z=0) -> x*y-z
>
>actually means?

I think I'm following you, but I'm not sure how much of it is
diagramic or meant to be actual code.
 
Are you suggesting to use the -> as a operator?  If so then this is
backwards to the = operator.  

    x*y-z <- (x,y,z=0)  

Would be more consistent to current syntax.  Then the <- would have
the meaning of translate instead of assign.  Or possibly an operator
for adapt?  Guido wants to use the -> in function definitions to
specify return value types.  Although I see problems with that too.

>Proposal:
>
>      (x,y=0) -> x*y   => ((x,y=0)->x*y)  (a,b) -> (x=a,y=b),a*b
>      (x,y=0) -> (x*y) => ((x,y=0)->(x*y))(a,b) -> (x=a*b,y=b)
>
>So (x,y=0) -> x*y is appending the result to the argument tuple.
>
>Remark: this is isomorph to
>
>     (x,y=0,res=None) -> ((x,y),x*y)
>
>but it becomes harder now to identify
>
>     (x,y,res=None) -> ((x,y),x*y)
>with
>     x*y
>
>Provide a compiler-hint:
>
>    (x,y,()) -> x*y

I'm not following you completely here. It appears your trying to
create a system to map different arguments to equations in a indirect
way.  


>Now we are ready for a few examples:
>
>
>default value:
>   (i) -> (0)               # i = 0
>
>inplace increment:
>   (i) -> i+1               # i = i+1
>
>conditional expression:
>   (i) -> i<3               # i,res = i,i<3

Lost me again, what is res?

>simple transformation:
>   (res) -> (res+i**2)      # res = res+i**2
>
>
>Define a While loop as a function:
>
>def While( par, cond, change, action):
>    par(None) # create default
>    res = 0
>    while cond(par)[1]:
>        action(res)
>        change(par)
>    return res
>
>Let's apply it to some tuple actions:
>
>While((i)->(0), (i)->i<3, (i)->(i+1), (res)->(res+i**2))
>
>and evaluate While stepwise:
>
>1. par(None)     <=> (i)->(0)(None)             # (i)   = (0)
>2. cond(par)[1]  <=> (i)->i<3(0)                # (i,c) = (0,True)
>3. action(res)   <=> (res) -> (res+i**2)(0)     # (res) = (0)
>4. change(par)   <=> (i)->(i+1)(0)              # (i)   = (1)
>5. cond(par)[1]  <=> (i)->i<3(1)                # (1,c) = (0,True)
>6. action(res)   <=> (res) -> (res+i**2)(0)     # (res) = (1)
>7. change(par)   <=> (i)->(i+1)(1)              # (i)   = (2)
>5. cond(par)[1]  <=> (i)->i<3(2)                # (2,c) = (0,True)
>6. action(res)   <=> (res) -> (res+i**2)(1)     # (res) = (5)
>7. change(par)   <=> (i)->(i+1)(2)              # (i)   = (3)
>5. cond(par)[1]  <=> (i)->i<3(2)                # (2,c) = (0,False)
>break
>
>=>  res = 5
>
>
>If we customize the other control flow primitives For and If it should
>be possible to create a little language only by using this primitives.
>
>It is obvious by definition of our While that we can replace arguments
>on the fly:
>
>conds = [(i)->i<3, (i)->i+2<7, (i)->i>=0]
>
>[ While((i)->(0), cond, (i)->(i+1), (res)->(res+i**2)) for cond in
>conds]
>
>=> [5,29,0]
>
>
>Wouldn't it be fun to use in Python?
>
>Only drawback: does not look like executable pseudo-code anymore :(
>
>
>Regards Kay

I think I get the gist of what you are trying to do, but I can't
follow it entirely.

Well it looks like an interesting puzzle, but you'll need to go a
little slower for some of us. I tired though. :)

Ron_Adam




More information about the Python-list mailing list