ternlog 0.1.0

Ternary logic operations on integer types
Documentation
#!/usr/bin/env python3

# Just bruteforce until we get the expression we want

REGS = 'x', 'y', 'z',
BIN_OPS = '&', '|', '^'


def permute(depth):
    yield from REGS
    if depth == 0:
        return
    yield from (f'(not {r})' for r in REGS)
    for lhs in permute(depth - 1):
        for rhs_d in range(depth):
            for rhs in permute(rhs_d):
                for op in BIN_OPS:
                    yield f'({lhs} {op} {rhs})'

def generate(table, prefix, depth):
    for expr in permute(depth):
        #print('    ', expr)
        f = eval(f'lambda x, y, z: {expr}')
        if all(f(x, y, z) == r for x, y, z, r in table):
            return expr.replace('(not x)', '!x').replace('(not y)', '!y').replace('(not z)', '!z')

#[*map(print, permute(1))]
#exit(0)

for tbl in range(256):
    table = tuple(((i & 4) != 0, (i & 2) != 0, (i & 1) != 0, (tbl & (1 << i)) != 0) for i in range(8))
    for d in range(10000):
        v = generate(table, '0', d)
        if v is not None:
            print('0b', bin(tbl)[2:].rjust(8, '0'), ' => ', v, ',', sep='')
            break