test(lua/threads): re-activate Lua thread examples

Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
Leonardo de Moura 2014-05-08 18:52:15 -07:00
parent 9676f48470
commit 21e3e22017
4 changed files with 19 additions and 82 deletions

View file

@ -1,37 +0,0 @@
import("util.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()
-- Create tactic t1 in a different Lua State.
-- So, t1 can be executed by a different execution
-- thread
local t1 = S:eval([[
counter1 = 0
return tactic(function(env, ios, s)
while true do
print("tactic 1")
counter1 = counter1 + 1
sleep(10)
end
end)
]])
counter2 = 0
local t2 = tactic(function(env, ios, s)
while true do
print("tactic 2")
counter2 = counter2 + 1
sleep(10)
end
end)
local T = (t1:par(t2)):try_for(500)
T:solve(env, ios, ctx, p)
assert(counter2 > 2)
S:eval([[ assert(counter1 > 2) ]])

View file

@ -1,33 +0,0 @@
import("util.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)

View file

@ -2,21 +2,32 @@ S1 = State()
S2 = State()
code = [[
function f(env, prefix, num, type)
local r = list_certified_definition()
for i = 1, num do
env:add_var(prefix .. "_" .. i, type)
r = list_certified_definition(type_check(env, mk_var_decl(prefix .. "_" .. i, type)), r)
end
return r
end
]]
S1:dostring(code)
S2:dostring(code)
e = environment()
e:add_var("N", Type())
local e = empty_environment()
e = add_decl(e, mk_var_decl("N", Type))
code2 = [[
e, prefix = ...
f(e, prefix, 1000, Const("N"))
return f(e, prefix, 1000, Const("N"))
]]
T1 = thread(S1, code2, e, "x")
T2 = thread(S2, code2, e, "y")
T1:wait()
T2:wait()
print(e)
local r1 = T1:wait()
local r2 = T2:wait()
while not r1:is_nil() do
e = e:add(r1:car())
r1 = r1:cdr()
end
while not r2:is_nil() do
e = e:add(r2:car())
r2 = r2:cdr()
end
assert(e:find("x_" .. 10))
assert(e:find("y_" .. 100))

View file

@ -9,17 +9,13 @@ local val = 10
S:eval([[ local f = ...; assert(f(1) == 11) ]], function (x) return x + val end)
S:eval([[ local o = ...; assert(o == name("a")) ]], name("a"))
S:eval([[ local o = ...; assert(o == Const("a")) ]], Const("a"))
S:eval([[ local o = ...; assert(is_context(o)) ]], context())
S:eval([[ local o = ...; assert(is_environment(o)) ]], environment())
S:eval([[ local o = ...; assert(is_environment(o)) ]], empty_environment())
S:eval([[ local o = ...; assert(o == mpz(100)) ]], mpz(100))
S:eval([[ local o = ...; assert(o == mpq(100)/3) ]], mpq(100)/3)
S:eval([[ local o = ...; assert(is_options(o)) ]], options())
S:eval([[ local o = ...; assert(is_sexpr(o)) ]], sexpr())
S:eval([[ local o = ...; assert(o:is_cons()) ]], sexpr(1, 2))
S:eval([[ local o = ...; assert(is_format(o)) ]], format("1"))
S:eval([[ local o = ...; assert(is_context_entry(o)) ]], context_entry("a", Const("T")))
S:eval([[ local o = ...; assert(is_local_entry(o)) ]], mk_lift(1, 2))
S:eval([[ local o = ...; assert(is_local_context(o)) ]], local_context())
S:eval([[ local o1, o2, o3 = ...; assert(is_sexpr(o1)); assert(is_name(o2)); assert(o3 == 10) ]], sexpr(), name("foo"), 10)
assert(not pcall(function() S:eval([[ x = ]]) end))
local T = thread(S, [[ local x = ...; return x + 10, x - 10 ]], 10)