Predicate in Programing Languages and Naming
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:
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, 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:
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
sigils war, magic chars in variable name
- Variable Naming: English Words Considered Harmful
- Parameter names start with phi φ, variable names start with xi ξ
- The Sigil War, Syntactic Indicator for Types of Function and Variable (2016)
- Elisp: DOLLAR SIGN $ and AT SIGN @ in Variable Name
- Predicate in Programing Languages and Naming
- Syntactic Meaning of Variable
- Perl: Variable Name Prefix (aka Sigil)
- Ruby: Variable Name Conventions
- PowerShell: Automatic Variables
- Clojure: Variable Name Conventions