lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Something like this seems to satisfy most if not all of those requirements.

local MT = {}

FALSE = setmetatable({-1, "FALSE"}, MT)
NA = setmetatable({0, "NA"}, MT)
TRUE = setmetatable({1, "TRUE"}, MT)

local FALSE = FALSE
local NA = NA
local TRUE = TRUE

local lookup = {
[-1] = FALSE,
[0] = NA,
[1] = TRUE,
}

local index = {}
function index.AND(a, b)
  return lookup[math.max(a[1], b[1])]
end
function index.OR(a, b)
  return lookup[math.min(a[1], b[1])]
end
function index.NOT(a)
  return lookup[-a[1]]
end
function index.XOR(a, b)
  return lookup[a[1] * b[1]]
end


function MT.__tostring(v) return v[2] end
MT.__mul = index.AND
MT.__add = index.OR
MT.__unm = index.NOT
MT.__pow = index.XOR
MT.__index = index




On Wed, Nov 17, 2010 at 3:22 PM, Axel Kittenberger <axkibe@gmail.com> wrote:
You could use table metamethods to overload * (and), + (or) and unary - (not).
Could you live with a look like this?

v3 = -(v1 * v2)   --- means not (v1 and v2)
if v3:TRUE() then
 ...
fi

On Wed, Nov 17, 2010 at 2:56 PM, Alexander Gladysh <agladysh@gmail.com> wrote:
> On Wed, Nov 17, 2010 at 14:56, Leo Razoumov <slonik.az@gmail.com> wrote:
>> On Wed, Nov 17, 2010 at 01:31, Alexander Gladysh <agladysh@gmail.com> wrote:
>>> On Wed, Nov 17, 2010 at 08:13, Doug Currie <doug.currie@gmail.com> wrote:
>>>> On Nov 16, 2010, at 11:51 PM, Alexander Gladysh wrote:
>
>>>>> I'm looking for the Lua equivalent for this:
>
>>>>> http://www.boost.org/doc/libs/1_44_0/doc/html/tribool.html
>
>>>> You can get a long way with using
>
>>>> true := 1.0
>>>> false := 0.0
>>>> indeterminate := NaN
>
>>> Nice!
>
>>> But NaN is not a first-class-citizen in Lua.
>
>>> I think that sooner or later I'll want to do t[indeterminate] = value.
>
>> How about the balanced ternary system:
>
>> tri_true= -1
>> tri_unknown= 0
>> tri_false= 1
>
>> function tri_and(x,y) return max(x,y) end
>> function tri_or(x,y) return min(x,y) end
>> function tri_not(x,y) return -x end
>> function tri_xor(x,y) return x*y end
>
> Looks good, thanks!
>
> Now I need to separate that from the actual numbers.
>
> That is, to make a new "Lua type" so, at least, I can check that
> function returned indeterminate, not proper 0. I'm aware that Lua
> lacks virtualization in some places, so I will not be able to make
> tribool looking like bool to user.
>
> But that's OK, I will generate most of the code that works with it
> automatically anyway. That is, I can afford to use some method calls
> instead of using standard Lua operators to work with tribools.
>
> Any suggestions on the module interface?
>
> Thanks,
> Alexander.
>
>