Jargon: Predicate in Programing Languages (2014)
What is Predicate in Computer Science
In programing language, “predicate” just means a function that return true or false.
In first order logic, “predicate” is a unary relation.
For example, p(x) means p(x) is true.
Note, p(x) is an assertion.
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 Programing Languages
Endin in p. (Emacs Lisp, Common Lisp)
in emacs lisp and Common Lisp, by convention, predicate function names end with “p”.
emacs lisp example:
boundpbuffer-modified-pconspfboundpfeaturepfile-directory-pfile-exists-pfile-readable-pfunctionpintegerplistpnumberpstringpsymbolpvectorpzerop
In emacs lisp, it is inconsistent.
Some ends in “-p”, some just “p”, and not all functions ending in p is a predicate.
E.g. {pop, defgroup, make-sparse-keymap, forward-sexp}
Endin in Question Mark. (Scheme Lisp, Clojure Lisp, Ruby)
in Scheme Lisp and Clojure Lisp, ruby, 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.
clojure example:
empty?even?
ruby example:
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?
Ending in Q. Wolfram Language
in Mathematica, predicate ends with “Q”, standing for Question.
Examples:
AlgebraicIntegerQAlgebraicUnitQArgumentCountQArrayQAtomQBinaryImageQCoprimeQDigitQDirectoryQDistributionDomainQDistributionParameterQEllipticNomeQEvenQExactNumberQFileExistsQFreeQHermitianMatrixQHypergeometricPFQImageQInexactNumberQIntegerQIntervalMemberQInverseEllipticNomeQIrreduciblePolynomialQLegendreQLetterQLinkConnectedQLinkReadyQListQLowerCaseQMachineNumberQMatchLocalNameQMatchQMatrixQMemberQNameQNumberQNumericQOddQOptionQOrderedQPartitionsQPolynomialQPositiveDefiniteMatrixQPossibleZeroQPrimePowerQPrimeQQHypergeometricPFQQuadraticIrrationalQRootOfUnityQSameQSatisfiableQSquareFreeQStringFreeQStringMatchQStringQSymmetricMatrixQSyntaxQTautologyQTensorQTrueQUnsameQUpperCaseQValueQVectorQ