Last active
January 8, 2022 17:22
-
-
Save evaporei/1d9d5e220b7bac79bc933812e94b7106 to your computer and use it in GitHub Desktop.
Boolean functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const assert = require('assert') | |
function test(title, fn) { | |
try { | |
fn() | |
console.log(`✅ '${title}' test passed`) | |
} catch (error) { | |
console.error(`❌ '${title}' test failed`) | |
throw error | |
} | |
} | |
const property = (fn1, fn2) => { | |
assert(fn1(false, false) === fn2(false, false)) | |
assert(fn1(true, false) === fn2(true, false)) | |
assert(fn1(false, true) === fn2(false, true)) | |
assert(fn1(true, true) === fn2(true, true)) | |
} | |
const and = (x, y) => x && y | |
const or = (x, y) => x || y | |
const not = x => !x | |
const const1 = () => true | |
const const0 = () => false | |
const x = (x, y) => x | |
const y = (x, y) => y | |
const notX = (x, y) => !x | |
const notY = (x, y) => !y | |
// 0 0 -> false | |
// 0 1 -> true | |
// 1 0 -> true | |
// 1 1 -> false | |
const xor = (x, y) => (x && !y) || (!x && y) | |
// 0 0 -> true | |
// 0 1 -> true | |
// 1 0 -> true | |
// 1 1 -> false | |
const nand = (x, y) => !(x && y) | |
// 0 0 -> true | |
// 0 1 -> false | |
// 1 0 -> false | |
// 1 1 -> false | |
const nor = (x, y) => !(x || y) | |
const equivalence = (x, y) => (x && y) || (!x && !y) | |
const yThenX = (x, y) => x || !y | |
const xThenY = (x, y) => !x || y | |
test('and', () => { | |
assert(and(false, false) === false) | |
assert(and(true, false) === false) | |
assert(and(false, true) === false) | |
assert(and(true, true) === true) | |
const notFromNand = x => | |
nand(x, x) | |
// NAND(A, A) === NOT(A) | |
const andFromNand = (x, y) => | |
notFromNand(nand(x, y)) | |
// NAND over the same value has the property of NOT | |
// so we calculate nand(x, y) and negate it, via | |
// passing it twice to NAND (equivalent to NOT) | |
property(and, andFromNand) | |
const andFromNor = (x, y) => | |
nor(nor(x, x), nor(y, y)) | |
property(and, andFromNor) | |
}) | |
test('or', () => { | |
assert(or(false, false) === false) | |
assert(or(true, false) === true) | |
assert(or(false, true) === true) | |
assert(or(true, true) === true) | |
const orFromNand = (x, y) => | |
nand(nand(x, x), nand(y, y)) | |
// negate the opposites of x and y | |
// NAND(A, A) === NOT(A) | |
// !(!x && !y) === x || y | |
property(or, orFromNand) | |
const notFromNor = x => | |
nor(x, x) | |
const orFromNor = (x, y) => | |
notFromNor(nor(x, y), nor(x, y)) | |
property(or, orFromNor) | |
}) | |
test('not', () => { | |
assert(not(false) === true) | |
assert(not(true) === false) | |
const notFromNand = x => | |
nand(x, x) | |
property(not, notFromNand) | |
const notFromNor = x => | |
nor(x, x) | |
property(not, notFromNor) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment