3. In Java
interface Predicate<X> {
public boolean check(X x);
}
boolean foreach<X>(Iterable<X> xs, Predicate<X> p) {
for (x: xs) if (!p.check(xs)) return false;
return true;
}
boolean exists<X>(Iterable<X> xs, Predicate<X> p) {
for (x: xs) if (p.check(xs)) return true;
return false;
}
4. In Scala
type Predicate[X] = X => Boolean
def foreach[X](xs: Iterable[X], p: Predicate[X]) = {
xs.foreach(p)
}
boolean exists[X](xs: Iterable[X], p: Predicate[X]) {
xs.exists(p)
}
5. In Scala
def foreach[X](xs: Iterable[X], p: X => Boolean) =
xs foreach p
boolean exists[X](xs: Iterable[X], p: X => Boolean) =
xs exists p
6. In Logic, take 1
● P1∨P2∨...∨Pn ->
∨1..nPi
∃i=1..nPi
● P1∧P2∧...∨Pn ->
∀i=1..nPi
∧1..nPi
7. In Logic, take 2: Example
● P1=Tet(a); P2=Small(b); P3=¬Red(c)
Not much to parameterize, all formulas are different…
● P1=Tet(a); P2=Tet(b); P3=Tet(c)
or:
● x1=a; x2=b; x3=c; Pi=Tet(xi)
∀i=1..nTet(xi) or ∃i=1..nTet(xi)
or:
● x1=a; x2=b; x3=c; Pi=Tet(xi)
(x)
∀x
in {a,b,c}
Tet(x) or ∃x
in {a,b,c}
Tet
8. In Logic, take 3
● Can we always enumerate arguments?
● e.g. all real numbers from [0,1]
We Need Variables!
9. In Logic, take 4: Introducing Variables
Anything that looks like an identifier
Even _ can do if we know what we mean
Scala:
List(1,2,3) exists (_ % 2 == 0)
10. Add Variables to Atomic Sentences
<atomic> ::= <predicateName>(<arguments>)
<predicateName> ::= <Capital>
<predicateName> ::= <Capital><letters>
<var> ::= /* choose a distinct set of words */
<name> ::= <objectName>|<var>
<arguments> ::= <name>|<arguments>,<name>
E.g. Left(x, a)
x is a variable
14. Formulas with Quantifiers, formally
<wff> ::= <atomic formula>
<wff> ::= ¬<wff>|(<wff>)
<wff> ::= <wff>v<wff>|<wff>∧<wff>
<wff> ::= <wff>→<wff>|<wff>↔<wff>
<wff> ::= ∀<var> <wff> // the var is bound here
<wff> ::= ∃<var> <wff> // the var is bound here
<sentence> ::= <wff /*where all vars are bound*/>
15. Satisfaction
A wff P(x) with an unbound variable x
is satisfied on a iff ⊨ P(a)
E.g. a small cube satisfies the formula
Cube(x) ∧ ¬ Large(x)
16. Satisfaction for Specific Formulas
Formula Kind How Satisfied
<atomic>
find an object or objects
¬P
find counterexample for P
PvQ
find something that satisfies at least one of
P and Q
P∧Q
find something that satisfies P and Q
∀x P
P is satisfied by all objects
∃x P
we can find an object that satisfies P
17. Problems with Domain
Formula Kind How Satisfied
<atomic>
find an object or objects
¬P
find counterexample for P
PvQ
find something that satisfies at least one of
P and Q
P∧Q
find something that satisfies P and Q
∀x P
P is satisfied by all objects
∃x P
we can find an object that satisfies P
18. Problems with Domain
● ∀x (Cube(x)∨Tet(x))
- how about 27? 28?
● ∃n (n*n > n*n*n)
- how about Claire’s cat?
19. Translating Aristotelian Forms
Aristotle says
We write
All P’s are Q’s
∀x (P(x) → Q(x))
Some P’s are Q’s
∃x (P(x) ∧ Q(x))
No P’s are Q’s
∀x (P(x) → ¬Q(x))
Some P’s are not Q’s
∃x (P(x) ∧ ¬Q(x))
How about
∃x (P(x) → Q(x))
Or, e.g., ∀y(Tet(y) → Small(y))