Skip to content

Instantly share code, notes, and snippets.

@pedrozath
Last active August 25, 2024 19:25
Show Gist options
  • Save pedrozath/5c905e567f1f935ad02bbe2e16b63361 to your computer and use it in GitHub Desktop.
Save pedrozath/5c905e567f1f935ad02bbe2e16b63361 to your computer and use it in GitHub Desktop.
A study onto Binary Connectives
LOGIC_GATE = -> (*truth_table) do
->(*args) { truth_table[args.join.to_i(2)] }
end
NOT = LOGIC_GATE[1,0]
AND = LOGIC_GATE[0,0,0,1]
OR = LOGIC_GATE[0,1,1,1]
# other logic gates can be composed with the above ones
# NAND = NOT >> AND
# NOR = NOT >> OR
connective = -> (expected_truth_table, truth_function) do
truth_table = expected_truth_table.map.with_index do |_, index|
p, q = *index.to_s(2).rjust(2, "0").chars.map(&:to_i)
output = truth_function[p, q]
end
unless truth_table == expected_truth_table
raise "#{truth_table.inspect} should be #{expected_truth_table}"
end
truth_function
end
contradiction = connective[[0,0,0,0], -> (p, q) { AND[p, NOT[p]] }]
tautology = connective[[1,1,1,1], contradiction >> NOT]
material_implication = connective[[1,1,0,1], -> (p, q) { OR[AND[NOT[p], 1], AND[p, q]] }]
material_non_implication = connective[[0,0,1,0], material_implication >> NOT]
converse_implication = connective[[1,0,1,1], -> (p, q) { OR[AND[NOT[q], 1], AND[p, q]] }]
converse_non_implication = connective[[0,1,0,0], converse_implication >> NOT]
conjunction = connective[[0,0,0,1], AND]
non_conjunction = connective[[1,1,1,0], AND >> NOT]
disjunction = connective[[0,1,1,1], OR]
non_disjunction = connective[[1,0,0,0], OR >> NOT]
proposition_p = connective[[0,0,1,1], -> (p, q) { p }]
negation_of_proposition_p = connective[[1,1,0,0], proposition_p >> NOT]
proposition_q = connective[[0,1,0,1], -> (p, q) { q }]
negation_of_proposition_q = connective[[1,0,1,0], proposition_q >> NOT]
equivalence = connective[[1,0,0,1], -> (p, q) { OR[AND[p, q], AND[NOT[p], NOT[q]]] }]
non_equivalence = connective[[0,1,1,0], equivalence >> NOT]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment