function with program counter

classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

function with program counter

Philipp Kraus-2
Hello,

I would like to use a list structure for calling Lua function. So some explanation.
I have got a function e.g.

function test({…})
     do first call
     do some print
     do second call
     do some print again
     if (a)
        do A
     else
        do B
     end
     ….
end

I have got also a "program counter" which is started by 1. On 1 its should run "do first call" on 2 "do some print" and so on.
I would like to use a Lua function, but I would like to run this function step-by-step. I can use a if construct to check the current
"program counter value" and run the correct command, but this is not very nice.

Did you have an idea for this problem?

Thanks

Phil
Reply | Threaded
Open this post in threaded view
|

Re: function with program counter

Tim Hill

On Sep 14, 2013, at 1:26 PM, Philipp Kraus <[hidden email]> wrote:

> Hello,
>
> I would like to use a list structure for calling Lua function. So some explanation.
> I have got a function e.g.
>
> function test({…})
>     do first call
>     do some print
>     do second call
>     do some print again
>     if (a)
>        do A
>     else
>        do B
>     end
>     ….
> end
>
> I have got also a "program counter" which is started by 1. On 1 its should run "do first call" on 2 "do some print" and so on.
> I would like to use a Lua function, but I would like to run this function step-by-step. I can use a if construct to check the current
> "program counter value" and run the correct command, but this is not very nice.
>
> Did you have an idea for this problem?
>
> Thanks
>
> Phil

I think what you are really talking about is a state machine, which is often handled by a switch statement. Since Lua lacks these, you end up resorting to a bunch of "if" statements doing one-by-one checks of your program counter, as you note.

The classic alternative to a switch statement for a state machine is a jump table, which is Lua terms is an array of functions. If you re-write each of your steps as a small function:

function first_call(…)
end

function some_print(…)
end

-- etc.

-- You can then create an array of these:

call_table = { first_call, some_print, … }

-- Now, you can re-write "test" to just call the appropriate function in the array:

function test(…)
        return call_table[program_counter](…)
end

-- And call it like this…

program_counter = 1
test(…)
program_counter = program_counter + 1
test(…)

-- Of course you can increment the program counter inside each call to test() if that suits your purpose, or even make it an upvalue.

--Tim






Reply | Threaded
Open this post in threaded view
|

Re: function with program counter

D. Matt Placek
In reply to this post by Philipp Kraus-2

On Sat, Sep 14, 2013 at 8:26 PM, Philipp Kraus <[hidden email]> wrote:
Hello,

I would like to use a list structure for calling Lua function. So some explanation.
I have got a function e.g.

function test({…})
     do first call
     do some print
     do second call
     do some print again
     ….
end

I have got also a "program counter" which is started by 1. On 1 its should run "do first call" on 2 "do some print" and so on.
I would like to use a Lua function, but I would like to run this function step-by-step. I can use a if construct to check the current
"program counter value" and run the correct command, but this is not very nice.

It's not clear why you have a program counter.  If you're just using the program counter to execute the function a bit at a time but in a linear manner, you might consider using a coroutine and avoid using a program counter/state variable altogether.

Reply | Threaded
Open this post in threaded view
|

Re: function with program counter

Andrew Starks
In reply to this post by Philipp Kraus-2


On Saturday, September 14, 2013, Philipp Kraus wrote:
Hello,

I would like to use a list structure for calling Lua function. So some explanation.
I have got a function e.g.

function test({…})
     do first call
     do some print
     do second call
     do some print again
     if (a)
        do A
     else
        do B
     end
     ….
end

I have got also a "program counter" which is started by 1. On 1 its should run "do first call" on 2 "do some print" and so on.
I would like to use a Lua function, but I would like to run this function step-by-step. I can use a if construct to check the current
"program counter value" and run the correct command, but this is not very nice.

Did you have an idea for this problem?

Thanks

Phil

We have a similar model, but use a function / filter graph, ala ltn12, but not implemented that way. 

Basically, you have a bunch of functions, but they're actually tables with the __call method defined. You connect them with an "add_target" method such that the return statement calls the next function or functions (in the case of multiparented graphs, where one function triggers multiple filters  down the line).

You can store a "step" value in the argument list to the connected function, if needed. This comes up for us when the graph is a tree of some variety. 

When there is one source, or starting function, you can pump the source: call the source function and trigger tail calls. 
If it is a tree with multiple children per parent, then we tend to call the final function (root / sink) and have it call its children, processing their return values and then passing the output back. Since all of the functions in the tree are doing the same thing, recursively, the effect is that you get the output in the order that you expected. Children that are not parents need sources, which might simply be functions that pass back raw data when they are called, which makes them look like plain filters to their parents. 

Hopefully this is helpful. :)

-Andrew
Reply | Threaded
Open this post in threaded view
|

Re: function with program counter

Jon Akhtar
In reply to this post by Tim Hill
Have you considered running the byte code from the listing file?

There are lots of steps, I looks like you mean statement by statement, but I am not sure what granularity of execution you need.

You can write the program using strings in a list then loadstring as you go. even. Otherwise you will have to be careful about upvalues and side-effects.


On Sat, Sep 14, 2013 at 8:29 PM, Tim Hill <[hidden email]> wrote:

On Sep 14, 2013, at 1:26 PM, Philipp Kraus <[hidden email]> wrote:

> Hello,
>
> I would like to use a list structure for calling Lua function. So some explanation.
> I have got a function e.g.
>
> function test({…})
>     do first call
>     do some print
>     do second call
>     do some print again
>     if (a)
>        do A
>     else
>        do B
>     end
>     ….
> end
>
> I have got also a "program counter" which is started by 1. On 1 its should run "do first call" on 2 "do some print" and so on.
> I would like to use a Lua function, but I would like to run this function step-by-step. I can use a if construct to check the current
> "program counter value" and run the correct command, but this is not very nice.
>
> Did you have an idea for this problem?
>
> Thanks
>
> Phil

I think what you are really talking about is a state machine, which is often handled by a switch statement. Since Lua lacks these, you end up resorting to a bunch of "if" statements doing one-by-one checks of your program counter, as you note.

The classic alternative to a switch statement for a state machine is a jump table, which is Lua terms is an array of functions. If you re-write each of your steps as a small function:

function first_call(…)
end

function some_print(…)
end

-- etc.

-- You can then create an array of these:

call_table = { first_call, some_print, … }

-- Now, you can re-write "test" to just call the appropriate function in the array:

function test(…)
        return call_table[program_counter](…)
end

-- And call it like this…

program_counter = 1
test(…)
program_counter = program_counter + 1
test(…)

-- Of course you can increment the program counter inside each call to test() if that suits your purpose, or even make it an upvalue.

--Tim







Reply | Threaded
Open this post in threaded view
|

Re: function with program counter

Andrew Starks
In reply to this post by Andrew Starks


On Saturday, September 14, 2013, Andrew Starks wrote:


On Saturday, September 14, 2013, Philipp Kraus wrote:
Hello,

I would like to use a list structure for calling Lua function. So some explanation.
I have got a function e.g.

function test({…})
     do first call
     do some print
     do second call
     do some print again
     if (a)
        do A
     else
        do B
     end
     ….
end

I have got also a "program counter" which is started by 1. On 1 its should run "do first call" on 2 "do some print" and so on.
I would like to use a Lua function, but I would like to run this function step-by-step. I can use a if construct to check the current
"program counter value" and run the correct command, but this is not very nice.

Did you have an idea for this problem?

Thanks

Phil

We have a similar model, but use a function / filter graph, ala ltn12, but not implemented that way. 

We have a bunch of functions, but they're actually tables with the __call method defined. They are connected with an "add_target" method such that the return statement calls the next function or functions (in the case of multiparented graphs, where one function triggers multiple filters  down the line).

You can store a "step" value in the argument list to the connected function, if needed. This comes up for us when the graph is a tree of some variety. 

When there is one source, or starting function, you can pump the source: call the source function and trigger tail calls. 
If it is a tree with multiple children per parent, then we tend to call the final function (root / sink) and have it call its children, processing their return values and then passing the output back. Since all of the functions in the tree are doing the same thing, recursively, the effect is that you get the output in the order that you expected. Children that are not parents need sources, which might simply be functions that pass back raw data when they are called, which makes them look like plain filters to their parents. 

Hopefully this is helpful. :)

-Andrew
Reply | Threaded
Open this post in threaded view
|

Re: function with program counter

Andrew Starks


On Saturday, September 14, 2013, Andrew Starks wrote:


On Saturday, September 14, 2013, Andrew Starks wrote:


On Saturday, September 14, 2013, Philipp Kraus wrote:
Hello,

I would like to use a list structure for calling Lua function. So some explanation.
I have got a function e.g.

function test({…})
     do first call
     do some print
     do second call
     do some print again
     if (a)
        do A
     else
        do B
     end
     ….
end

I have got also a "program counter" which is started by 1. On 1 its should run "do first call" on 2 "do some print" and so on.
I would like to use a Lua function, but I would like to run this function step-by-step. I can use a if construct to check the current
"program counter value" and run the correct command, but this is not very nice.

Did you have an idea for this problem?

Thanks

Phil

We have a similar model, but use a function / filter graph, ala ltn12, but not implemented that way. 

We have a bunch of functions, but they're actually tables with the __call method defined. They are connected with an "add_target" method such that the return statement calls the next function or functions (in the case of multiparented graphs, where one function triggers multiple filters  down the line).

You can store a "step" value in the argument list to the connected function, if needed. This comes up for us when the graph is a tree of some variety. 

When there is one source, or starting function, you can pump the source: call the source function and trigger tail calls. 
If it is a tree with multiple children per parent, then we tend to call the final function (root / sink) and have it call its children, processing their return values and then passing the output back. Since all of the functions in the tree are doing the same thing, recursively, the effect is that you get the output in the order that you expected. Children that are not parents need sources, which might simply be functions that pass back raw data when they are called, which makes them look like plain filters to their parents. 

Hopefully this is helpful. :)

-Andrew
Reply | Threaded
Open this post in threaded view
|

function with program counter

Andrew Starks
Sorry for the multiple emails! I've been pocket sending. I drafted an answer, drafted another, went away, pulled my phone out to finish and notice that I had sent both versions three times with my pocket. 

To top it off, Tim and Matt's answer are correct. I miss understood the problem as wanting to compose a problem amongst small functions and call them in steps. 

Man I wish I could "unsend". I will be more careful. 

-Andrew
Reply | Threaded
Open this post in threaded view
|

Re: function with program counter

Philipp Kraus
In reply to this post by Tim Hill
On 2013-09-15 00:29:41 +0000, Tim Hill said:

>
> On Sep 14, 2013, at 1:26 PM, Philipp Kraus
> <[hidden email]> wrote:
>
>> Hello,
>>
>> I would like to use a list structure for calling Lua function. So some
>> explanation.
>> I have got a function e.g.
>>
>> function test({…})
>> do first call
>> do some print
>> do second call
>> do some print again
>> if (a)
>> do A
>> else
>> do B
>> end
>> ….
>> end
>>
>> I have got also a "program counter" which is started by 1. On 1 its
>> should run "do first call" on 2 "do some print" and so on.
>> I would like to use a Lua function, but I would like to run this
>> function step-by-step. I can use a if construct to check the current
>> "program counter value" and run the correct command, but this is not very nice.
>>
>> Did you have an idea for this problem?
>>
>> Thanks
>>
>> Phil
>
> I think what you are really talking about is a state machine, which is
> often handled by a switch statement. Since Lua lacks these, you end up
> resorting to a bunch of "if" statements doing one-by-one checks of your
> program counter, as you note.

Yes you're right, I'm thinking about a state-machine, each "function
call" must be atomic, but a function call can be run many other
function calls.

The idea is, in which way can I do this nice in Lua and IMHO Andrew
Starks has got a greate idea

You're greate

Phil



Reply | Threaded
Open this post in threaded view
|

Re: function with program counter

Philipp Kraus
In reply to this post by Andrew Starks
On 2013-09-15 00:56:32 +0000, Andrew Starks said:

> We have a similar model, but use a function / filter graph, ala ltn12,
> but not implemented that way. 
>
> Basically, you have a bunch of functions, but they're actually tables
> with the __call method defined. You connect them with an "add_target"
> method such that the return statement calls the next function or
> functions (in the case of multiparented graphs, where one function
> triggers multiple filters  down the line).
>
> You can store a "step" value in the argument list to the connected
> function, if needed. This comes up for us when the graph is a tree of
> some variety. 
>
> When there is one source, or starting function, you can pump the
> source: call the source function and trigger tail calls. 
> If it is a tree with multiple children per parent, then we tend to call
> the final function (root / sink) and have it call its children,
> processing their return values and then passing the output back. Since
> all of the functions in the tree are doing the same thing, recursively,
> the effect is that you get the output in the order that you expected.
> Children that are not parents need sources, which might simply be
> functions that pass back raw data when they are called, which makes
> them look like plain filters to their parents. 
>
> Hopefully this is helpful. :)

Hi Andrew,

you're also great. In combination with Tim's state machine idea this is
greate. I think I will use a metatable with a field for my program
counter and store the function into this table an can run all steps

Great

Phil



Reply | Threaded
Open this post in threaded view
|

Re: function with program counter

Jayanth Acharya
In reply to this post by Andrew Starks
Was going to suggest the "hsm_statechart" library which I found on luarocks site, and thought to be a way to implement state-machines in lua (ala iMatix Libero, not sure if anyone here used it). However, came across this reply, which seems very interesting. Any chance of being able to post some small sample that explains the method to the uninitiated -- time permitting ?


On Sun, Sep 15, 2013 at 6:26 AM, Andrew Starks <[hidden email]> wrote:


On Saturday, September 14, 2013, Philipp Kraus wrote:
Hello,

I would like to use a list structure for calling Lua function. So some explanation.
I have got a function e.g.

function test({…})
     do first call
     do some print
     do second call
     do some print again
     if (a)
        do A
     else
        do B
     end
     ….
end

I have got also a "program counter" which is started by 1. On 1 its should run "do first call" on 2 "do some print" and so on.
I would like to use a Lua function, but I would like to run this function step-by-step. I can use a if construct to check the current
"program counter value" and run the correct command, but this is not very nice.

Did you have an idea for this problem?

Thanks

Phil

We have a similar model, but use a function / filter graph, ala ltn12, but not implemented that way. 

Basically, you have a bunch of functions, but they're actually tables with the __call method defined. You connect them with an "add_target" method such that the return statement calls the next function or functions (in the case of multiparented graphs, where one function triggers multiple filters  down the line).

You can store a "step" value in the argument list to the connected function, if needed. This comes up for us when the graph is a tree of some variety. 

When there is one source, or starting function, you can pump the source: call the source function and trigger tail calls. 
If it is a tree with multiple children per parent, then we tend to call the final function (root / sink) and have it call its children, processing their return values and then passing the output back. Since all of the functions in the tree are doing the same thing, recursively, the effect is that you get the output in the order that you expected. Children that are not parents need sources, which might simply be functions that pass back raw data when they are called, which makes them look like plain filters to their parents. 

Hopefully this is helpful. :)

-Andrew

Reply | Threaded
Open this post in threaded view
|

Re: function with program counter

Andrew Starks
On Sun, Sep 15, 2013 at 7:10 AM, Jayanth Acharya <[hidden email]> wrote:

> Was going to suggest the "hsm_statechart" library which I found on luarocks
> site, and thought to be a way to implement state-machines in lua (ala iMatix
> Libero, not sure if anyone here used it). However, came across this reply,
> which seems very interesting. Any chance of being able to post some small
> sample that explains the method to the uninitiated -- time permitting ?
>
>
> On Sun, Sep 15, 2013 at 6:26 AM, Andrew Starks <[hidden email]>
> wrote:
>>
>>
<snip>


Jayanth,

I'd be happy to, except that my solution wouldn't solve this problem.
I believe that Tim and D. Matt provided good answers.

Whether a single-lined function chain or a tree of functions is what
you're interested in, you should start here:

http://w3.impa.br/~diego/software/luasocket/ltn12.html

If you have more questions, post them, and I'd be only too eager to
throw in some more examples.

BTW: After some time with Lua, you'll then yearn for LTN12 example to
be implemented with coroutines, because it simplifies the design a bit
more and makes it even more powerful.


-Andrew

Reply | Threaded
Open this post in threaded view
|

Re: function with program counter

Steve Litt
In reply to this post by Tim Hill
On Sat, 14 Sep 2013 17:29:41 -0700
Tim Hill <[hidden email]> wrote:


> I think what you are really talking about is a state machine, which
> is often handled by a switch statement. Since Lua lacks these, you
> end up resorting to a bunch of "if" statements doing one-by-one
> checks of your program counter, as you note.
>
> The classic alternative to a switch statement for a state machine is
> a jump table, which is Lua terms is an array of functions. If you
> re-write each of your steps as a small function:
>
> function first_call(…)
> end
>
> function some_print(…)
> end
>
> -- etc.
>
> -- You can then create an array of these:
>
> call_table = { first_call, some_print, … }
>
> -- Now, you can re-write "test" to just call the appropriate function
> in the array:
>
> function test(…)
> return call_table[program_counter](…)
> end
>
> -- And call it like this…
>
> program_counter = 1
> test(…)
> program_counter = program_counter + 1
> test(…)
>
> -- Of course you can increment the program counter inside each call
> to test() if that suits your purpose, or even make it an upvalue.
>
> --Tim

This is *such* a cool idea, I should have thought of it myself. And
doing it Lua makes it even cooler because states can have meaningful
names like "close_bracket_encountered" as keys for the functions. And
even better, the array of functions could have an element called "data"
that would house data being passed between the various states, for
those times when you care more about coding ease than about modularity,
but still don't want to use global variables.

SteveT

Steve Litt                *  http://www.troubleshooters.com/
Troubleshooting Training  *  Human Performance

Reply | Threaded
Open this post in threaded view
|

Re: function with program counter

Andrew Starks
In reply to this post by Jon Akhtar


On Saturday, September 14, 2013, Jon Akhtar wrote:
Have you considered running the byte code from the listing file?

There are lots of steps, I looks like you mean statement by statement, but I am not sure what granularity of execution you need.

You can write the program using strings in a list then loadstring as you go. even. Otherwise you will have to be careful about upvalues and side-effects.


On Sat, Sep 14, 2013 at 8:29 PM, Tim Hill <<a href="javascript:_e({}, &#39;cvml&#39;, &#39;drtimhill@gmail.com&#39;);" target="_blank">drtimhill@...> wrote:

On Sep 14, 2013, at 1:26 PM, Philipp Kraus <<a href="javascript:_e({}, &#39;cvml&#39;, &#39;philipp.kraus@tu-clausthal.de&#39;);" target="_blank">philipp.kraus@...> wrote:

> Hello,
>
> I would like to use a list structure for calling Lua function. So some explanation.
> I have got a function e.g.
>
> function test({…})
>     do first call
>     do some print
>     do second call
>     do some print again
>     if (a)
>        do A
>     else
>        do B
>     end
>     ….
> end
>
> I have got also a "program counter" which is started by 1. On 1 its should run "do first call" on 2 "do some print" and so on.
> I would like to use a Lua function, but I would like to run this function step-by-step. I can use a if construct to check the current
> "program counter value" and run the correct command, but this is not very nice.
>
> Did you have an idea for this problem?
>
> Thanks
>
> Phil

I think what you are really talking about is a state machine, which is often handled by a switch statement. Since Lua lacks these, you end up resorting to a bunch of "if" statements doing one-by-one checks of your program counter, as you note.

The classic alternative to a switch statement for a state machine is a jump table, which is Lua terms is an array of functions. If you re-write each of your steps as a small function:

function first_call(…)
end

function some_print(…)
end

-- etc.

-- You can then create an array of these:

call_table = { first_call, some_print, … }

-- Now, you can re-write "test" to just call the appropriate function in the array:

function test(…)
        return call_table[program_counter](…)
end

-- And call it like this…

program_counter = 1
test(…)
program_counter = program_counter + 1
test(…)

-- Of course you can increment the program counter inside each call to test() if that suits your purpose, or even make it an upvalue.

--Tim








Given the already implemented solution: roughly 25 lines of code, two hidden table accesses, one hidden function call and 0 manual book keeping to do, these suggestions seem like they go further away from the stated goal for seeking alternatives.

So I'm back to asking: what am I missing? 
Reply | Threaded
Open this post in threaded view
|

Re: function with program counter

Marc Lepage
If you just want to perform the function in sections, how about coroutines?


Something like:

function test({...})
    do first call
    coroutine.yield()
    do some print
    coroutine.yield()
    do second call
    coroutine.yield()
    do some print again
    coroutine.yield()
    if (a)
       do A
    else
       do B
    end
    coroutine.yield()
    ...
end

co = coroutine.create(test)

counter = 0
while coroutine.status(co) ~= 'dead' do
    counter = counter + 1
    coroutine.resume(co)
end



On Tue, Sep 17, 2013 at 8:00 PM, Andrew Starks <[hidden email]> wrote:


On Saturday, September 14, 2013, Jon Akhtar wrote:
Have you considered running the byte code from the listing file?

There are lots of steps, I looks like you mean statement by statement, but I am not sure what granularity of execution you need.

You can write the program using strings in a list then loadstring as you go. even. Otherwise you will have to be careful about upvalues and side-effects.


On Sat, Sep 14, 2013 at 8:29 PM, Tim Hill <[hidden email]> wrote:

On Sep 14, 2013, at 1:26 PM, Philipp Kraus <[hidden email]> wrote:

> Hello,
>
> I would like to use a list structure for calling Lua function. So some explanation.
> I have got a function e.g.
>
> function test({…})
>     do first call
>     do some print
>     do second call
>     do some print again
>     if (a)
>        do A
>     else
>        do B
>     end
>     ….
> end
>
> I have got also a "program counter" which is started by 1. On 1 its should run "do first call" on 2 "do some print" and so on.
> I would like to use a Lua function, but I would like to run this function step-by-step. I can use a if construct to check the current
> "program counter value" and run the correct command, but this is not very nice.
>
> Did you have an idea for this problem?
>
> Thanks
>
> Phil

I think what you are really talking about is a state machine, which is often handled by a switch statement. Since Lua lacks these, you end up resorting to a bunch of "if" statements doing one-by-one checks of your program counter, as you note.

The classic alternative to a switch statement for a state machine is a jump table, which is Lua terms is an array of functions. If you re-write each of your steps as a small function:

function first_call(…)
end

function some_print(…)
end

-- etc.

-- You can then create an array of these:

call_table = { first_call, some_print, … }

-- Now, you can re-write "test" to just call the appropriate function in the array:

function test(…)
        return call_table[program_counter](…)
end

-- And call it like this…

program_counter = 1
test(…)
program_counter = program_counter + 1
test(…)

-- Of course you can increment the program counter inside each call to test() if that suits your purpose, or even make it an upvalue.

--Tim








Given the already implemented solution: roughly 25 lines of code, two hidden table accesses, one hidden function call and 0 manual book keeping to do, these suggestions seem like they go further away from the stated goal for seeking alternatives.

So I'm back to asking: what am I missing?