logic¶
we deal with binary truths like: false or true, ✅ or ❌, F or T, 0 or 1, ⊤ or ⊥, OFF or ON, etc. we can create functions on these truth values.
introduction¶
we can construct all the possible gates in a very neat way: with n inputs, we have 2^n possible permutations. for 2^n permutations, we have 22n gates.
with n = 0, we have 2 nullary gates:
| output | name |
|---|---|
| ❌ | false |
| ✅ | true |
a function with zero inputs is not a function anymore. these two are stored as constants.
with n = 1, we have 4 unary gates:
| F | T | name |
|---|---|---|
| ❌ | ❌ | |
| ❌ | ✅ | same as just taking the input as-is (identity function) |
| ✅ | ❌ | not |
| ✅ | ✅ |
with n = 2, we have 16 binary gates:
| FF | FT | TF | TT | name |
|---|---|---|---|---|
| ❌ | ❌ | ❌ | ❌ | |
| ❌ | ❌ | ❌ | ✅ | and |
| ❌ | ❌ | ✅ | ❌ | fst |
| ❌ | ❌ | ✅ | ✅ | (same as just taking second argument) |
| ❌ | ✅ | ❌ | ❌ | snd |
| ❌ | ✅ | ❌ | ✅ | (same as just taking first argument) |
| ❌ | ✅ | ✅ | ❌ | xor |
| ❌ | ✅ | ✅ | ✅ | or |
| ✅ | ❌ | ❌ | ❌ | nor |
| ✅ | ❌ | ❌ | ✅ | xnor |
| ✅ | ❌ | ✅ | ❌ | same as just taking complement of first argument |
| ✅ | ❌ | ✅ | ✅ | con |
| ✅ | ✅ | ❌ | ❌ | same as just taking complement of second argument |
| ✅ | ✅ | ❌ | ✅ | imp |
| ✅ | ✅ | ✅ | ❌ | nand |
| ✅ | ✅ | ✅ | ✅ |
with n = 3, we suddenly have 256 ternary gates. since there are so many, and because they can be composed from binary gates anyway, daamath does not maintain gates of arity > 2.
API implementation¶
not(value):¶
negation is involutive. if you apply it twice, it gives you the original value.
and(first, second):¶
being associative, conjunction has a variadic version `all`. can also be thought of as intersection of sets
or(first, second):¶
being associative, disjunction has a variadic version `any`. can also be thought of as union of sets.
xor(first, second):¶
being associative, exclusive disjunction has a variadic version `parity_odd`. can also be thought of as symmonric difference of sets
imp(first, second):¶
material implication is distinctly directional. can also be thought of as difference of sets
con(first, second):¶
same as `imp(second, first)`. can also be thought of as difference of sets
nand(first, second):¶
not(and(first, second))
nor(first, second):¶
not(or(first, second))
nxor(first, second):¶
not(xor(first, second)). being associative, it has a variadic version `parity_even`
nimp(first, second):¶
not(imp(first, second))
ncon(first. second):¶
not(con(first, second))
type support¶
in fact, these logical gates can be applied to more than just logical values. we can apply them to integers in 2's complement bitwise, and also to sets. for example, in python, daamath.and supports bool, int, and set as input. in C, dm_and supports bool and int, applying them logically or bitwise respectively.
notes¶
nxor is used instead of xnor to preserve consistency.