Lesson 2 of 15

Predicate Logic & Quantifiers

From Propositions to Predicates

Predicate logic (first-order logic) extends propositional logic with predicates — statements about objects — and quantifiers that range over domains.

A predicate P(x)P(x) is a statement about xx that becomes a proposition when xx is given a value. The two quantifiers are:

xD, P(x)(universal: P holds for every x in D)\forall x \in D,\ P(x) \quad \text{(universal: P holds for every x in D)} xD, P(x)(existential: P holds for at least one x in D)\exists x \in D,\ P(x) \quad \text{(existential: P holds for at least one x in D)}

Negation of quantifiers (De Morgan for quantifiers): ¬(x, P(x))x, ¬P(x)\neg(\forall x,\ P(x)) \equiv \exists x,\ \neg P(x) ¬(x, P(x))x, ¬P(x)\neg(\exists x,\ P(x)) \equiv \forall x,\ \neg P(x)

def for_all(domain, predicate):
    return all(predicate(x) for x in domain)

def exists(domain, predicate):
    return any(predicate(x) for x in domain)

# Every number in {1..5} is positive
print(for_all(range(1, 6), lambda x: x > 0))  # True
# Some number in {1..5} equals 3
print(exists(range(1, 6), lambda x: x == 3))  # True

Nested Quantifiers

Order matters: xy, P(x,y)\forall x\, \exists y,\ P(x,y) differs from yx, P(x,y)\exists y\, \forall x,\ P(x,y).

Your Task

Implement for_all(domain, predicate) and exists(domain, predicate) using Python's built-in all and any.

Pyodide loading...
Loading...
Click "Run" to execute your code.