|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- truthtables.lua - print truth tables (for propositional calculus)
-- http://angg.twu.net/LUA/truthtables.lua.html
-- (find-angg "LUA/truthtables.lua")
--
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
-- Version: 2012feb11
-- This is a quick hack inspired by a discussion with Alexandre
-- Costa-Leite. I only tested it interactively.
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
--]]
E = "P Q -> P -> Q ->"
expr = split(E)
PP(expr)
V = {}
V["P"] = 1
V["Q"] = 0
PP(V)
stack = {}
push = function (x) stack[#stack + 1] = x end
pop = function (x) o = stack[#stack]; stack[#stack] = nil; return o end
imp = function (P, Q) return P <= Q and 1 or 0 end
IMP = function () local Q, P = pop(), pop(); push(imp(P, Q)) end
AND = function () local Q, P = pop(), pop(); push(P * Q) end
OR = function () local Q, P = pop(), pop(); push(max(P, Q)) end
NOT = function () push(1 - pop()) end
OPS = {}
OPS["->"] = IMP
OPS["or"] = OR
OPS["&"] = AND
OPS["NOT"] = NOT
PP(OPS)
interpret = function ()
stack = {}
for _,op in ipairs(expr) do
if OPS[op] then OPS[op]() else push(V[op]) end
end
return stack[1]
end
tabela = function (E)
expr = split(E)
for p=0,1 do V.P=p
for q=0,1 do V.Q=q
print("P="..V.P.." Q="..V.Q.." ("..E..")="..interpret())
end
end
end
tabela "P Q ->"
tabela "P Q &"
tabela "P Q or"
tabela "P Q -> P &"
tabela "P Q & P ->"
tabela "P Q -> P -> Q ->"