Implication

The boolean operators you get in most programming languages (and, or, not, xor...) tend to leave out implication.

Implication:

 p | q |
---|---|---
 T | T | T
 T | F | F
 F | T | T
 F | F | T

When it's useful

Say you have a configuration that has different modes, and each mode has a set of options. You want to make sure that options for mode A are only used when mode is actually A.

You could do this:

if (optionForA is set) {
    assert(mode === 'A', 'optionForA is only valid when mode is A')
}

but that's not that nice. Implication helps!

assert((optionForA is set) → (mode === 'A'),
    'optionForA is only valid when mode is A')

Doing this with the standard programming language boolean operators works, but is not easy for me to read:

assert(!(optionForA is set) || (mode === 'A'),
    'optionForA is only valid when mode is A')

Ternaries can help, but the `: true` side gives me a WTF moment every time

assert((optionForA is set) ? (mode === 'A') : true,
    'optionForA is only valid when mode is A')

You can't define new operators in javascript, but you can make functions:

const implication = (p, q) => p ? q : true;

assert(implication(optionForA is set, mode === 'A'),
    'optionForA is only valid when mode is A')

If you like taking things too far like I do, you can search through unicode character lists for an arrow-like letter that is a valid JS name:

const 𐅙 = (p, q) => p ? q : true;

assert(𐅙(optionForA is set, mode === 'A'),
    'optionForA is only valid when mode is A')

Unfortunately javascript is strict, so we can't avoid evaluating q. (Real operators in JS like && and || can, via short-circuit evaluation)

false && die();   // doesn't die
true  || die();   // still alive!
𐅙(false, die());  // x_x