Predicate in Programing Languages and Naming
Meaning of “Predicate” in Logic and Computer Science
in math and programing, there's the term “predicate”. In first order logic, “predicate” is a unary relation. For example, p(x)
means p(x) is true. In pseudo-code, you are asserting eval(f(x)) == True
.
But in programing language, “predicate” just means a function that return true or false.
this is important and often confusing. Here's a concrete example. Suppose you want to say that 3 is a integer:
- in predicate logic, you express it by:
isInteger(3)
- in typical programing languages, you can't make a assertion. Rather, you express something like this:
if (isInteger(3) == True) {print("good")} else {error("something's wrong")}
In the above example, “isInteger” is the predicate, but their meaning is critically different.
Naming Convention of Predicate in Computer Languages
Emacs Lisp and Common Lisp
in emacs lisp and Common Lisp, by convention, predicate function names end with “p”. For example, in emacs lisp you have:
- boundp
- buffer-modified-p
- consp
- fboundp
- featurep
- file-directory-p
- file-exists-p
- file-readable-p
- functionp
- integerp
- listp
- numberp
- stringp
- symbolp
- vectorp
- zerop
in emacs lisp, not all functions ending in p is a predicate.
e.g. {pop
, defgroup
, make-sparse-keymap
, forward-sexp
}
Scheme Lisp and Clojure Lisp
in Scheme Lisp and Clojure Lisp, the convention is to name predicate ending with a question mark “?”.
this is better. Because the question mark is more intuitive. The “p” is incomprehensible, and the term “predicate” came from history of logic.
Wolfram Language
in Mathematica, they end with “Q”, standing for Question.
Mathematica identifiers allow letters only, and builtin names always start with a capital letter. So, all builtin names have this CamelCaseStyle.
So, they cannot use question mark ?. And because p for predicate is not intuitive, thus Q, for Question.
examples of Mathematica predicate:
- AlgebraicIntegerQ
- AlgebraicUnitQ
- ArgumentCountQ
- ArrayQ
- AtomQ
- BinaryImageQ
- CoprimeQ
- DigitQ
- DirectoryQ
- DistributionDomainQ
- DistributionParameterQ
- EllipticNomeQ
- EvenQ
- ExactNumberQ
- FileExistsQ
- FreeQ
- HermitianMatrixQ
- HypergeometricPFQ
- ImageQ
- InexactNumberQ
- IntegerQ
- IntervalMemberQ
- InverseEllipticNomeQ
- IrreduciblePolynomialQ
- LegendreQ
- LetterQ
- LinkConnectedQ
- LinkReadyQ
- ListQ
- LowerCaseQ
- MachineNumberQ
- MatchLocalNameQ
- MatchQ
- MatrixQ
- MemberQ
- NameQ
- NumberQ
- NumericQ
- OddQ
- OptionQ
- OrderedQ
- PartitionsQ
- PolynomialQ
- PositiveDefiniteMatrixQ
- PossibleZeroQ
- PrimePowerQ
- PrimeQ
- QHypergeometricPFQ
- QuadraticIrrationalQ
- RootOfUnityQ
- SameQ
- SatisfiableQ
- SquareFreeQ
- StringFreeQ
- StringMatchQ
- StringQ
- SymmetricMatrixQ
- SyntaxQ
- TautologyQ
- TensorQ
- TrueQ
- UnsameQ
- UpperCaseQ
- ValueQ
- VectorQ
〔see Wolfram Language Tutorial〕
Ruby
in Ruby, it picked up from Scheme Lisp, and ends with “?”. For example, here's predicate methods for integer:
- zero?
- odd?
- even?
- integer?
- eql?
- real?
- nonzero?
- between?
- nil?
- tainted?
- untrusted?
- frozen?
- instance_variable_defined?
- instance_of?
- kind_of?
- is_a?
- respond_to?
- respond_to_missing?
- equal?
〔see Ruby Tutorial〕