lean2/tests/lua/threads/tactic2.lua
Leonardo de Moura ce674d2d43 feat(library/tactic): execute Lua tactics using coroutines
This is very important when several Lua tactics are implemented in the
same Lua State object.  In this case, even if we use the par
combinator, a Lua tactic will block the other Lua tactics running in
the same Lua State object.

With this commit, a Lua tactic can use yield to allow other tactics
in the same State object to execute.

Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
2013-11-28 13:09:33 -08:00

32 lines
839 B
Lua

local env = environment()
local ios = io_state()
local Bool = Const("Bool")
env:add_var("p", Bool)
env:add_var("q", Bool)
local p, q = Consts("p, q")
local ctx = context()
S = State()
-- tactics t1 and t2 uses yield to implement cooperative
-- multitasking
local counter1 = 0
local t1 = tactic(function(env, ios, s)
while true do
counter1 = counter1 + 1
coroutine.yield()
end
end)
local counter2 = 0
local t2 = tactic(function(env, ios, s)
while true do
counter2 = counter2 + 1
coroutine.yield()
end
end)
local T = (t1:par(t2)):try_for(150)
T:solve(env, ios, ctx, p)
print(counter1, counter2)
assert(counter1 > 2)
assert(counter2 > 2)