Logic
The 27 definitions of the logic library, each with its Epsil spelling, its MathJSON name, its signature and its full description.
Logical operators
&& (And), || (Or) and ! (Not) are operators; their precedence is
in the table of Operators. The other connectives
are functions: xor, nand, nor, implies and equivalent. xor,
nand and nor take any number of operands: xor is true when an odd
number of them are true, nand is the negation of && and nor the
negation of ||.
[xor(true, false), nand(true, true), nor(false, false)]
// ➔ [true,false,true]
xor(true, true, true)
// ➔ true
&&, ||, nand, nor and implies short-circuit: their operands
are evaluated left to right, in the order written, and evaluation stops at
the first operand that decides the result: the first false for && and
nand, the first true for || and nor, a false antecedent for
implies. The remaining operands are not evaluated, and for that reason
these operands are never reordered. xor and equivalent cannot
short-circuit: every operand affects the result, so an operand that fails
makes the result an error.
function g(x) { 1/x }
[implies(false, g("a") > 0), nand(false, g("a") > 0)]
// ➔ [true,true]
function g(x) { 1/x }
nor(true, g("a") > 0)
// ➔ false
function g(x) { 1/x }
equivalent(true, g("a") > 0)
// ➔ error: Runtime error: expected `number`, got `string` at `a`
An operand that fails does not stop a short-circuit operator, and an operand after it can still decide. Logic operators gives the rule, and the element-wise form, in which an operand that is a list of booleans makes the result a list.
[true, false] && [true, true]
// ➔ [true,false]
Boolean simplification
A boolean expression is simplified with these laws when it is evaluated:
| Law | Rule | Epsil |
|---|---|---|
| Absorption | A \land (A \lor B) \to A | A && (A || B) is A |
| Absorption | A \lor (A \land B) \to A | A || (A && B) is A |
| Idempotence | A \land A \to A | A && A is A |
| Idempotence | A \lor A \to A | A || A is A |
| Complementation | A \land \lnot A \to \bot | A && !A is false |
| Complementation | A \lor \lnot A \to \top | A || !A is true |
| Identity | A \land \top \to A | A && true is A |
| Identity | A \lor \bot \to A | A || false is A |
| Domination | A \land \bot \to \bot | A && false is false |
| Domination | A \lor \top \to \top | A || true is true |
| Double negation | \lnot\lnot A \to A | !!A is A |
[A && (A || B), A || (A && B), A && A && B]
// ➔ [A,A,A && B]
[A && !A, A || !A, A && true, A || false, A && false, A || true, !!A]
// ➔ [false,true,A,A,false,true,A]
First-order logic
Quantifiers
The quantifiers are functions: forAll (universal), exists
(existential), existsUnique (exactly one), and the negated quantifiers
notForAll and notExists. Each takes a condition and a proposition. The
condition is the variable being quantified over, a symbol, or x in S,
which also gives the set of values that the variable can take. The variable
is local to the proposition.
Quantifier evaluation
A quantifier is decided by trying each value when the condition is
x in S and S is finite: a set, a list, or a range a..b, the integers
from a to b. The domain has at most 1000 elements. Over a larger range,
the elements are not tried: the quantifier is decided only from the bounds of
the range, when they decide it, and otherwise stays unevaluated.
An interval(a, b) is a set of real numbers, with bounds that are open or
closed, so its elements cannot be tried. For the integers from a to b,
write the range a..b. A quantifier over an interval stays unevaluated.
[exists(x in interval(1, 2), x == 1), exists(n in 1..2, n == 1)]
// ➔ [Exists(Element(x, Interval(1, 2)), x == 1),true]
[forAll(x in set(1, 2, 3), x > 0), exists(x in [1, 2, 3], x > 2)]
// ➔ [true,true]
forAll(n in 1..10, n < 10)
// ➔ false
exists(n in 1..1000, n == 1000)
// ➔ true
exists(n in 1..2000, n == 1500)
// ➔ Exists(Element(n, 1..2000), n == 1500)
[forAll(n in 1..2000, n > 0), forAll(n in 1..2000, n < 1500)]
// ➔ [true,ForAll(Element(n, 1..2000), n < 1500)]
Over an empty domain, forAll is true and exists is false.
[forAll(x in set(), x > 0), exists(x in set(), x > 0)]
// ➔ [true,false]
Nested quantifiers of the same kind are evaluated over the Cartesian product of their domains.
forAll(x in set(1, 2), forAll(y in set(1, 2), x + y > 0))
// ➔ true
exists(x in set(1, 2), exists(y in set(1, 2), x + y == 4))
// ➔ true
When the domain is not finite, or there is no domain, these simplifications
apply: a quantifier whose proposition is true or false is that value,
and a quantifier whose proposition does not contain the variable is that
proposition. Otherwise the quantifier stays unevaluated, unless the
proposition can be decided for every value.
[forAll(x, true), exists(x, false), forAll(x, y > 0)]
// ➔ [true,false,0 < y]
forAll(x, x + 1 > x)
// ➔ true
exists(x, x^2 == 1)
// ➔ Exists(x, x^2 == 1)
Negated quantifiers
notForAll(c, p) is true when p fails for at least one value, and
notExists(c, p) when p holds for no value. They are evaluated like
forAll and exists. ! applied to a quantifier is kept as a negation.
[notForAll(x in set(1, 2, 3), x > 1), notExists(x in set(1, 2, 3), x > 5)]
// ➔ [true,true]
!forAll(x, x > 1)
// ➔ !ForAll(x, 1 < x)
LaTeX syntax for logic
Constants
| MathJSON | LaTeX | Notation |
|---|---|---|
True | \top | \top |
True | \mathsf{T} | \mathsf{T} |
True | \operatorname{True} | \operatorname{True} |
False | \bot | \bot |
False | \mathsf{F} | \mathsf{F} |
False | \operatorname{False} | \operatorname{False} |
LaTeX logical operators
| MathJSON | LaTeX | Notation | Description |
|---|---|---|---|
And | p \land q | p \land q | Conjunction |
And | p \wedge q | p \wedge q | |
And | p \operatorname{and} q | p \operatorname{and} q | |
Or | p \lor q | p \lor q | Disjunction |
Or | p \vee q | p \vee q | |
Or | p \operatorname{or} q | p \operatorname{or} q | |
Xor | p \veebar q | p \veebar q | Exclusive or |
Nand | p \barwedge q | p \barwedge q | Not and |
Nor | p ⊽ q | Not or | |
Not | \lnot p | \lnot p | Negation |
Not | \neg p | \neg p | |
Not | \operatorname{not}(p) | \operatorname{not}(p) | |
Equivalent | p \iff q | p \iff q | Equivalence |
Equivalent | p \Leftrightarrow q | p \Leftrightarrow q | |
Equivalent | p \leftrightarrow q | p \leftrightarrow q | |
Equivalent | p \Longleftrightarrow q | p \Longleftrightarrow q | |
Equivalent | p \longleftrightarrow q | p \longleftrightarrow q | |
Implies | p \implies q | p \implies q | Implication |
Implies | p \Rightarrow q | p \Rightarrow q | |
Implies | p \Longrightarrow q | p \Longrightarrow q | |
Implies | p \longrightarrow q | p \longrightarrow q | |
Proves | p \vdash q | p \vdash q | Provability |
Entails | p \vDash q | p \vDash q | Entailment |
Satisfies | p \models q | p \models q | Satisfaction |
Equivalent serializes as \iff, and Nor as ⊽. \equiv is not a
notation of Equivalent: it parses as IdenticallyEqual, the mathematical
identity operator.
Operator precedence
The logical operators bind looser than the comparison and arithmetic
operators, so x = 1 \lor y = 2 parses as (x = 1) \lor (y = 2).
| Precedence | Operators | Example |
|---|---|---|
| 880 | Not (\lnot, \neg) | \lnot p |
| 245 | Comparisons (=, <, >, \leq, \geq, \neq) | x = 1 |
| 240 | Set relations (\subset, \subseteq, \in, …) | x \in S |
| 235 | And (\land, \wedge) | p \land q |
| 232 | Xor, Nand, Nor | p \veebar q |
| 230 | Or (\lor, \vee) | p \lor q |
| 220 | Implies (\implies, \Rightarrow) | p \implies q |
| 219 | Equivalent (\iff, \Leftrightarrow) | p \iff q |
| 200 | Quantifiers (\forall, \exists) | \forall x, P(x) |
p \land q \lor rparses as(p \land q) \lor r.p \lor q \implies rparses as(p \lor q) \implies r.\lnotapplies only to the atom that follows it:\lnot p \land qparses as(\lnot p) \land q. To negate a conjunction, write\lnot(p \land q).
Arrow notation
\to and \rightarrow are reserved for the notation of a mapping, as in
f: A \to B, and parse as To, not Implies. For an implication, use
\implies, \Rightarrow or \Longrightarrow.
Quantifier scope
A quantifier binds tightly, as is usual in first-order logic: its scope is
the formula that immediately follows it and stops at a logical connective.
\forall x. P(x) \implies Q(x) parses as (∀x. P(x)) ⇒ Q(x):
["Implies", ["ForAll", "x", ["Predicate", "P", "x"]], ["Q", "x"]]
P(x), inside the quantifier, is a Predicate; Q(x), outside it, is an
ordinary function application. To include the connective in the scope, use
parentheses: \forall x. (P(x) \implies Q(x)) parses as
["ForAll", "x", ["Implies", ["Predicate", "P", "x"], ["Predicate", "Q", "x"]]]
The LaTeX parser of the JavaScript API has an option that makes the scope extend to the end of the expression instead; see the Logic guide.
Definitions
Each definition is listed under its Epsil spelling (the MathJSON name when it has none), with its signature in the engine's type syntax. The Standard Library page is the one-page index of every category.
And
(broadcastable<boolean>+) -> boolean
Logical conjunction (AND): true when all operands are true. Short-circuits: operands are evaluated left to right and evaluation stops at the first False.
MathJSON: And • LaTeX: \land
boole
(broadcastable<boolean>) -> integer
- argument 1 — The proposition; one that is neither
TruenorFalseleaves the result unevaluated. Sample:x > 0.
Return 1 if the argument is true, 0 otherwise. Also known as the Iverson bracket
boole(3 > 2)
// ➔ 1
boole([True, False, True])
// ➔ [1,0,1]
MathJSON: Boole • LaTeX: [, ]
equivalent
(broadcastable<boolean>, broadcastable<boolean>) -> boolean
- argument 1 — The first proposition. Sample:
x > 0. - argument 2 — The second proposition. Sample:
x >= 1.
Logical equivalence (if and only if): true when both operands have the same truth value.
equivalent(True, False)
// ➔ false
equivalent(p, p)
// ➔ true
MathJSON: Equivalent • LaTeX: \iff
exists
(value, boolean) -> boolean
- argument 1 — The bound variable, or an
Elementclause that gives it a domain; over a finite domain the quantifier tests each value, otherwise it stays unevaluated unless the predicate decides it. Sample:x in [1, 2, 3]. - argument 2 — The predicate on the bound variable. Sample:
x > 0.
Existential quantifier (there exists): true when the predicate holds for at least one value.
exists(Element(x, [1, 2, 3]), x > 2)
// ➔ true
exists is a logical quantifier, not a collection function: it takes a
condition and a proposition, not a collection and a predicate, and it binds a
variable. To ask whether an element of a collection satisfies a predicate,
use any.
The first argument introduces the variable: a symbol, or x in S. With a
finite S, the proposition is decided by trying each element.
exists(x in set(5, 2, 10, 18), x > 15)
// ➔ true
exists(x in set(5, 2, 10), x > 15)
// ➔ false
exists(x, x > 15)
// ➔ Exists(x, 15 < x)
The variable is local to the proposition and shadows a symbol of the same name outside it, so a value assigned to that symbol does not enter the proposition.
let x = 100
exists(x in set(5, 2, 10), x > 15)
// ➔ false
See also notExists and
existsUnique.
exists(x, p), with no domain, stays unevaluated unless the proposition
does not contain x or can be decided for every value: see
Quantifier evaluation.
MathJSON: Exists • LaTeX: \exists
["Exists", condition, proposition]. The condition is a symbol or
["Element", variable, domain].
["Exists", ["Element", "x", ["Set", 5, 2, 10, 18]], ["Greater", "x", 15]]
// ➔ "True"
["Exists", "x", proposition] quantifies over all values of x.
["Exists", "x", ["Equal", ["Square", "x"], 1]]
["Exists", ["Element", "x", "RealNumbers"], ["Equal", ["Square", "x"], 1]]
In LaTeX, the condition and the proposition are separated by a comma, a
colon or \mid, or the proposition is in parentheses after the condition:
\exists x, x^2 = 1, \exists x: x^2 = 1, \exists x \mid x^2 = 1 and
\exists x(x^2 = 1) are the same expression.
existsUnique
(value, boolean) -> boolean
- argument 1 — The bound variable, or an
Elementclause that gives it a domain; over a finite domain the quantifier tests each value, otherwise it stays unevaluated unless the predicate decides it. Sample:x in [1, 2, 3]. - argument 2 — The predicate on the bound variable. Sample:
x > 0.
Unique existential quantifier (there exists exactly one value satisfying the predicate).
existsUnique(Element(x, [1, 2, 3]), x > 2)
// ➔ true
existsUnique(Element(x, [1, 2, 3]), x > 1)
// ➔ false
existsUnique(x in S, p) is true when exactly one element of S
satisfies p. It is evaluated over a finite domain like
exists.
existsUnique(x in set(1, 2, 3), x == 2)
// ➔ true
existsUnique(x in set(1, 2, 3), x > 1)
// ➔ false
existsUnique(x, x > 1)
// ➔ ExistsUnique(x, 1 < x)
MathJSON: ExistsUnique • LaTeX: \exists!
["ExistsUnique", condition, proposition].
["ExistsUnique", ["Element", "x", ["Set", 1, 2, 3]], ["Equal", "x", 2]]
// ➔ "True"
In LaTeX, \exists! x, x^2 = 1.
False
constant boolean
The boolean truth value false.
MathJSON: False • LaTeX: \bot
forAll
(value, boolean) -> boolean
- argument 1 — The bound variable, or an
Elementclause that gives it a domain; over a finite domain the quantifier tests each value, otherwise it stays unevaluated unless the predicate decides it. Sample:x in [1, 2, 3]. - argument 2 — The predicate on the bound variable. Sample:
x > 0.
Universal quantifier (for all): true when the predicate holds for every value.
forAll(Element(x, [1, 2, 3]), x > 0)
// ➔ true
forAll(Element(n, Range(1, 10)), n^2 >= n)
// ➔ true
forAll is a logical quantifier, not a collection function. Like
exists, it binds the variable that its first
argument introduces, and with x in S over a finite S it is decided by
trying each element. To ask whether every element of a collection satisfies
a predicate, use all.
forAll(x in set(5, 2, 10, 18), x > 0)
// ➔ true
forAll(x in set(5, 2, 10, 18), x > 5)
// ➔ false
See also notForAll.
A proposition that holds for every value is decided without a domain:
forAll(x, x + 1 > x)
// ➔ true
MathJSON: ForAll • LaTeX: \forall
["ForAll", condition, proposition].
["ForAll", ["Element", "x", ["Set", 5, 2, 10, 18]], ["Greater", "x", 0]]
// ➔ "True"
In LaTeX, \forall x \in \{1, 2, 3\}, x > 0 parses to
["ForAll", ["Element", "x", ["Set", 1, 2, 3]], ["Greater", "x", 0]].
The condition is a symbol, or ["Element", variable, domain].
["ForAll", "x", ["Greater", ["Add", "x", 1], "x"]]
// ➔ "True"
["ForAll", ["Element", "x", "RealNumbers"], ["Greater", ["Square", "x"], 0]]
In LaTeX, the condition and the proposition are separated by a comma, a
colon or \mid, or the proposition is in parentheses after the condition:
\forall x, x + 1 > x, \forall x: x + 1 > x, \forall x \mid x + 1 > x
and \forall x(x + 1 > x) are the same expression. The condition can give
the domain: \forall x \in \R, x + 1 > x.
implies
(antecedent: broadcastable<boolean>, consequent: broadcastable<boolean>) -> boolean
antecedent— The condition; when it isFalse, the result isTrueand the consequent is not evaluated. Sample:x > 1.consequent— The proposition that must hold when the antecedent does. Sample:x > 0.
Logical implication: false only when the antecedent is true and the consequent is false. Short-circuits: a False antecedent decides (True) without evaluating the consequent.
implies(True, False)
// ➔ false
implies(False, x)
// ➔ true
MathJSON: Implies • LaTeX: \implies
isSatisfiable
(boolean) -> boolean
- argument 1 — The boolean expression; with more than 20 variables, or with an operand that is not a symbol or a connective (a comparison such as
x > 0), the result stays unevaluated. Sample:p && !q.
Return True when some assignment of truth values to the variables of a boolean expression makes it true, False otherwise.
The search tries every assignment: O(2^n) for n variables, at most 20 variables.
isSatisfiable(And(p, Not(p)))
// ➔ false
isSatisfiable(And(p, Or(q, Not(p))))
// ➔ true
Satisfiability and tautology
An expression is satisfiable when an assignment of truth values to its
variables makes it true, and a tautology when every assignment makes it
true. isSatisfiable and isTautology try every assignment. With more
than 20 variables, they stay unevaluated.
isSatisfiable(A && !A)
// ➔ false
isSatisfiable(A || B)
// ➔ true
isSatisfiable(A || !A)
// ➔ true
A contradiction is not satisfiable; a tautology is.
MathJSON: IsSatisfiable
["IsSatisfiable", expression].
["IsSatisfiable", ["And", "A", ["Not", "A"]]]
// ➔ "False"
isTautology
(boolean) -> boolean
- argument 1 — The boolean expression; with more than 20 variables, or with an operand that is not a symbol or a connective (a comparison such as
x > 0), the result stays unevaluated. Sample:p || !p.
Return True when a boolean expression is true for every assignment of truth values to its variables, False otherwise.
The search tries every assignment: O(2^n) for n variables, at most 20 variables.
isTautology(Or(p, Not(p)))
// ➔ true
isTautology(Or(p, q))
// ➔ false
Like isSatisfiable, isTautology
tries every assignment, and stays unevaluated with more than 20 variables.
isTautology(A || !A)
// ➔ true
isTautology(equivalent(!!A, A))
// ➔ true
isTautology(equivalent(!(A && B), !A || !B))
// ➔ true
isTautology(implies(A && implies(A, B), B))
// ➔ true
isTautology(A)
// ➔ false
MathJSON: IsTautology
["IsTautology", expression].
["IsTautology", ["Or", "A", ["Not", "A"]]]
// ➔ "True"
kroneckerDelta
(value+) -> integer
- arguments 1, … — A value compared with the others; the result stays unevaluated when the difference of two values is not a constant. Sample:
2.
Return 1 if the arguments are equal, 0 otherwise. With a single argument n, this is δ_{n,0}: 1 if n = 0, 0 otherwise.
kroneckerDelta(2, 2)
// ➔ 1
kroneckerDelta(2, 3)
// ➔ 0
MathJSON: KroneckerDelta • LaTeX: \delta_
minimalCNF
(boolean) -> boolean
- argument 1 — The boolean expression to minimize; with more than 12 variables, or with an operand that is not a symbol or a connective (a comparison such as
x > 0), the result stays unevaluated. Sample:(a || b) && (a || !b).
Return a minimal conjunctive normal form (CNF) of a boolean expression: an AND of ORs of its prime implicates with the fewest clauses (for a large expression, the search for the fewest can stop at a budget and give a few more).
Uses the Quine-McCluskey algorithm, at most 12 variables.
minimalCNF(And(Or(a, b), Or(a, Not(b))))
// ➔ a
minimalCNF finds the prime implicates of the expression and selects a
minimal cover of them: the result is a CNF with the fewest disjunctions.
toCNF does not guarantee that. See
Prime implicants and minimal normal forms.
minimalCNF((A || B) && (A || !B))
// ➔ A
minimalCNF(xor(A, B))
// ➔ (A || B) && (!A || !B)
MathJSON: MinimalCNF
["MinimalCNF", expression].
["MinimalCNF", ["And", ["Or", "A", "B"], ["Or", "A", ["Not", "B"]]]]
// ➔ "A"
minimalDNF
(boolean) -> boolean
- argument 1 — The boolean expression to minimize; with more than 12 variables, or with an operand that is not a symbol or a connective (a comparison such as
x > 0), the result stays unevaluated. Sample:(a && b) || (a && !b) || (!a && b).
Return a minimal disjunctive normal form (DNF) of a boolean expression: an OR of ANDs of its prime implicants with the fewest terms (for a large expression, the search for the fewest can stop at a budget and give a few more).
Uses the Quine-McCluskey algorithm, at most 12 variables.
minimalDNF(Or(And(a, b), And(a, Not(b)), And(Not(a), b)))
// ➔ b || a
minimalDNF finds the prime implicants of the expression and selects a
minimal cover of them: the result is a DNF with the fewest conjunctions.
toDNF does not guarantee that. See
Prime implicants and minimal normal forms.
minimalDNF((A && B) || (A && !B) || (!A && B))
// ➔ B || A
minimalDNF((A && B) || (A && !B))
// ➔ A
minimalDNF((A && B) || (!A && C) || (B && C))
// ➔ !A && C || A && B
minimalDNF(A || !A)
// ➔ true
MathJSON: MinimalDNF
["MinimalDNF", expression].
["MinimalDNF", ["Or", ["And", "A", "B"], ["And", "A", ["Not", "B"]]]]
// ➔ "A"
nand
(broadcastable<boolean>+) -> boolean
- arguments 1, … — A proposition; the operands after the first one that is
Falseare not evaluated. Sample:x > 0.
Logical NAND: the negation of AND (n-ary). Short-circuits: operands are evaluated left to right and evaluation stops at the first False.
nand(True, True)
// ➔ false
nand(True, False)
// ➔ true
MathJSON: Nand • LaTeX: \barwedge
nor
(broadcastable<boolean>+) -> boolean
- arguments 1, … — A proposition; the operands after the first one that is
Trueare not evaluated. Sample:x > 0.
Logical NOR: the negation of OR (n-ary). Short-circuits: operands are evaluated left to right and evaluation stops at the first True.
nor(False, False)
// ➔ true
nor(False, True)
// ➔ false
MathJSON: Nor • LaTeX: ⊽
Not
(broadcastable<boolean>) -> boolean
Logical negation (NOT).
MathJSON: Not • LaTeX: \lnot
notExists
(value, boolean) -> boolean
- argument 1 — The bound variable, or an
Elementclause that gives it a domain; over a finite domain the quantifier tests each value, otherwise it stays unevaluated unless the predicate decides it. Sample:x in [1, 2, 3]. - argument 2 — The predicate on the bound variable. Sample:
x > 0.
Negated existential quantifier (there does not exist): true when the predicate holds for no value.
notExists(Element(x, [1, 2, 3]), x > 5)
// ➔ true
MathJSON: NotExists • LaTeX: \lnot\exists
notForAll
(value, boolean) -> boolean
- argument 1 — The bound variable, or an
Elementclause that gives it a domain; over a finite domain the quantifier tests each value, otherwise it stays unevaluated unless the predicate decides it. Sample:x in [1, 2, 3]. - argument 2 — The predicate on the bound variable. Sample:
x > 0.
Negated universal quantifier (not for all): true when the predicate fails for at least one value.
notForAll(Element(x, [1, 2, 3]), x > 1)
// ➔ true
MathJSON: NotForAll • LaTeX: \lnot\forall
Or
(broadcastable<boolean>+) -> boolean
Logical disjunction (OR): true when at least one operand is true. Short-circuits: operands are evaluated left to right and evaluation stops at the first True.
MathJSON: Or • LaTeX: \lor
Predicate
(symbol, value+) -> boolean
Apply a predicate to arguments, returning a boolean
Predicates
A predicate is a function whose value is a boolean. In a quantifier, a predicate can be any function of the program:
function P(x) { x > 0 }
forAll(x in set(1, 2), P(x))
// ➔ true
function P(x) { x > 0 }
exists(x in set(-1, -2), P(x))
// ➔ false
Predicate(P, x) is the predicate P applied to x, kept as a formula: it
is not evaluated, even when P is a function.
function P(x) { x > 0 }
Predicate(P, 2)
// ➔ Predicate(P, 2)
MathJSON: Predicate
["Predicate", name, ...args] distinguishes the application of a predicate
from a function call in a formula of first-order logic.
["Predicate", "P", "x"]
["Predicate", "Q", "a", "b"]
It serializes to LaTeX as a function application: P(x).
In LaTeX, inside the scope of a quantifier, a single uppercase letter
followed by parentheses parses as a Predicate: \forall x, P(x) is
["ForAll", "x", ["Predicate", "P", "x"]], and \exists x, Q(x, y) is
["Exists", "x", ["Predicate", "Q", "x", "y"]]. Outside a quantifier, P(x)
parses as a function application, ["P", "x"], so D(f, x) is the
derivative D and N(\sqrt{10}) the numeric evaluation N. Inside a
quantifier these names are predicates too: \forall x, D(x) is
["ForAll", "x", ["Predicate", "D", "x"]]. A predicate with a name of more
than one letter, or a lowercase one, is a function that the program
declares.
primeImplicants
(boolean) -> list
- argument 1 — The boolean expression; with more than 12 variables, or with an operand that is not a symbol or a connective (a comparison such as
x > 0), the result stays unevaluated. Sample:(a && b) || (a && !b).
Return the list of the prime implicants of a boolean expression: the minimal conjunctions of literals that imply it.
Uses the Quine-McCluskey algorithm, at most 12 variables.
primeImplicants(Or(And(a, b), And(Not(a), c), And(b, c)))
// ➔ [!a && c,b && c,a && b]
Prime implicants and minimal normal forms
primeImplicants, primeImplicates, minimalDNF and minimalCNF use the
Quine-McCluskey algorithm. With more than 12 variables, they stay
unevaluated.
A prime implicant of an expression is a conjunction of literals that implies the expression and that is no longer an implicant when any literal is removed from it. The result is a list of them.
primeImplicants((A && B) || (A && !B))
// ➔ [A]
primeImplicants(A || B)
// ➔ [B,A]
primeImplicants(xor(A, B))
// ➔ [!A && B,A && !B]
primeImplicants((A && B) || (!A && C) || (B && C))
// ➔ [!A && C,B && C,A && B]
A contradiction has no prime implicants.
primeImplicants(A && !A)
// ➔ []
MathJSON: PrimeImplicants
["PrimeImplicants", expression].
["PrimeImplicants", ["Xor", "A", "B"]]
// ➔ ["List", ["And", ["Not", "A"], "B"], ["And", "A", ["Not", "B"]]]
primeImplicates
(boolean) -> list
- argument 1 — The boolean expression; with more than 12 variables, or with an operand that is not a symbol or a connective (a comparison such as
x > 0), the result stays unevaluated. Sample:a && b.
Return the list of the prime implicates of a boolean expression: the minimal disjunctions of literals that it implies.
Uses the Quine-McCluskey algorithm, at most 12 variables.
primeImplicates(And(Or(a, b), Or(Not(a), c)))
// ➔ [a || b,b || c,!a || c]
A prime implicate of an expression is a disjunction of literals that the expression implies and that is no longer an implicate when any literal is removed from it. Prime implicates are the dual of prime implicants: they are the clauses of a minimal CNF. The result is a list of them. See Prime implicants and minimal normal forms.
primeImplicates(A && B)
// ➔ [A,B]
primeImplicates(A || B)
// ➔ [A || B]
primeImplicates(xor(A, B))
// ➔ [A || B,!A || !B]
MathJSON: PrimeImplicates
["PrimeImplicates", expression].
["PrimeImplicates", ["And", "A", "B"]]
// ➔ ["List", "A", "B"]
toCNF
(boolean) -> boolean
- argument 1 — The boolean expression to convert. Sample:
(a && b) || c.
Convert a boolean expression to conjunctive normal form (CNF), an AND of ORs.
toCNF(Or(And(a, b), c))
// ➔ (a || c) && (b || c)
toCNF(implies(p, q))
// ➔ !p || q
Normal forms
A literal is a variable or its negation. The conjunctive normal form
(CNF) of an expression is a conjunction (&&) of disjunctions (||) of
literals, such as (A || B) && (!A || C). The disjunctive normal form
(DNF) is a disjunction of conjunctions of literals, such as
A && B || !A && C.
toCNF applies:
- De Morgan's laws:
\lnot(A \land B) \equiv \lnot A \lor \lnot B - Distribution:
(A \land B) \lor C \equiv (A \lor C) \land (B \lor C) - Implication elimination:
A \to B \equiv \lnot A \lor B - Equivalence elimination:
A \leftrightarrow B \equiv (\lnot A \lor B) \land (\lnot B \lor A)
toCNF((A && B) || C)
// ➔ (A || C) && (B || C)
toCNF(implies(A, B))
// ➔ !A || B
toCNF(!(A && B))
// ➔ !A || !B
toCNF(equivalent(A, B))
// ➔ (!A || B) && (!B || A)
toCNF(xor(A, B))
// ➔ (A || B) && (!A || !B)
The result is not always the shortest CNF: see
minimalCNF.
MathJSON: ToCNF
["ToCNF", expression].
["ToCNF", ["Or", ["And", "A", "B"], "C"]]
// ➔ ["And", ["Or", "A", "C"], ["Or", "B", "C"]]
toDNF
(boolean) -> boolean
- argument 1 — The boolean expression to convert. Sample:
(a || b) && c.
Convert a boolean expression to disjunctive normal form (DNF), an OR of ANDs.
toDNF(And(Or(a, b), c))
// ➔ a && c || b && c
toDNF applies:
- De Morgan's laws:
\lnot(A \lor B) \equiv \lnot A \land \lnot B - Distribution:
(A \lor B) \land C \equiv (A \land C) \lor (B \land C) - Implication and equivalence elimination, as
toCNFdoes
toDNF((A || B) && C)
// ➔ A && C || B && C
toDNF(!(A || B))
// ➔ !A && !B
toDNF(implies(A, B))
// ➔ !A || B
The result is not always the shortest DNF: see
minimalDNF.
MathJSON: ToDNF
["ToDNF", expression].
["ToDNF", ["And", ["Or", "A", "B"], "C"]]
// ➔ ["Or", ["And", "A", "C"], ["And", "B", "C"]]
True
constant boolean
The boolean truth value true.
MathJSON: True • LaTeX: \top
truthTable
(boolean) -> list
- argument 1 — The boolean expression; with more than 10 variables, or with an operand that is not a symbol or a connective (a comparison such as
x > 0), the result stays unevaluated. Sample:p && q.
Return the truth table of a boolean expression, as a list of rows.
The first row holds the names of the variables and "Result"; each other row holds one assignment of truth values and the value of the expression.
O(2^n) rows for n variables, at most 10 variables.
truthTable(And(p, q))
// ➔ [["p","q","Result"],[false,false,false],[false,true,false],[true,false,false],[true,true,true]]
The result is a list of rows. The first row holds the names of the
variables, in alphabetical order, followed by "Result". Each other row
holds one assignment of the variables and the value of the expression,
from all False to all True. With more than 10 variables (1024 rows),
truthTable stays unevaluated.
truthTable(A && B)
// ➔ [["A","B","Result"],[false,false,false],[false,true,false],[true,false,false],[true,true,true]]
truthTable(!A)
// ➔ [["A","Result"],[false,true],[true,false]]
MathJSON: TruthTable
["TruthTable", expression]. The names in the first row are strings.
["TruthTable", ["And", "A", "B"]]
// ➔ ["List", ["List", "'A'", "'B'", "'Result'"],
// ["List", "False", "False", "False"], ["List", "False", "True", "False"],
// ["List", "True", "False", "False"], ["List", "True", "True", "True"]]
xor
(broadcastable<boolean>+) -> boolean
- arguments 1, … — A proposition; an absent operand reads as
False. Sample:x > 0.
Exclusive or: true when an odd number of operands are true
xor(True, False)
// ➔ true
xor(True, False, True)
// ➔ false
MathJSON: Xor • LaTeX: \veebar