Being a hobbyist coder, I'm lacking some fundamental knowledge. For the last couple days I've been reading some stuff and the word "predicate" keeps reappearing. I'd very much appreciate an explanation on the subject.
12 Answers 12
The definition of a predicate, which can be found online in various sources such as here, is:
A logical expression which evaluates to TRUE or FALSE, normally to direct the execution path in code.
Referencing: Software Testing. By Mathew Hayden (dead link / unknown origin)
In programming, a predicate is a function which returns either true or false for some input.
Most commonly (I guess) used in the context of higher-order function. E.g. filter is a function in many languages which takes a predicate and a list as arguments, and returns the items in the list for which the predicate is true.
Example in javascript:
function lessThanTen(x) { return x < 10; }
[1,7,15,22].filter(lessThanTen) --> [1,7]
The function lessThanTen is the predicate here, which is applied to each item in the list.
A predicate isn't simply an expression that evaluates to true or false, there's more to it. The term "predicate" is used to refer to an expression that determines whether something is true or false. Or in other words, it makes an assertion and returns true or false based on that.
For example (in C#):
/*this is a predicate, as it's sole purpose is to make some
assertion about something.*/
bool IsNameBob(string name)
{
return name == "Bob";
}
/*Whereas this is not a predicate, as it's performing an action
then evaluating to true if it succeeds. */
bool DoSomethingCool() {
try
{
ImDoingSomethingCool();
}
catch
{
return false;
}
return true;
}
I understand what I've put here is purely a difference in semantics, but that's what this question was about right? Semantics?
const references, and whose return type is a bool.In non programing terms; a question. Typically a general question with place holders (like it and them) that can be asked of many things.
- Is it red?
- Is it a dog?
- Is it owned by them?
A basic evaluation that results in a boolean1 value. It often refers to a function or object that represents an evaluation of this type.
1: boolean used loosely, not necessarily referring to variables declared bool or boolean.
First let's take a look at a regular dictionary and see what it says about what a predicate is:
Oxford American Dictionary(1980):
n. a part of a sentence that says something about the grammatical subject, as "is short" in "life is short"
Here is another sentence: "John is tall." the predicate is "is tall". As you can see it modifies or describes the subject, another term that is similar to predicate is adjective. In essence it's a modifier.
IBM's technology glossary provides several definitions but, the one fits best is this one:
An expression used as part of a filter, consisting of a data item, an operator, and a value
Here is an example using SQL:
SELECT name
FROM tableA
WHERE name = "john";
The predicate in this code would be name = "john". It has all the components of the IBM definition and also fits with the regular definition of predicate. The subject being name and the predicate being name = "john".
Comments
It is probably useful to consider the grammatical meaning of the concept to extrapolate the programming concept.
In traditional grammar, a predicate is one of the two main parts of a sentence (the other being the subject, which the predicate modifies). For the simple sentence "John [is yellow]," John acts as the subject, and is yellow acts as the predicate, a subsequent description of the subject headed with a verb.
In current linguistic semantics, a predicate is an expression that can be true of something. Thus, the expressions "is yellow" or "is like broccoli" are true of those things that are yellow or like broccoli, respectively. This notion is closely related to the notion of a predicate in formal logic, which includes more expressions than the former one, like, for example, nouns and some kinds of adjectives.
In logic terms:
An operator in logic which returns either true or false.
from MathWorld
Comments
The best S.O. answer around predicates, that I have found, is on a duplicate question.
To summarize, in natural languages a predicate is the part of the sentence that describes a subject.
Jane is tall
Jane is the subject and is tall is the predicate.
In computer science we are not interested in asserting a fact about a subject but rather testing if something is true or false.
jane.isTall();
Here jane is some object with a predicate method that will return either true or false.
Comments
I don't know if I'm speaking in the correct context, but there is a Predicate class in C# which is essentially a delegate which, given an item, determines whether or not the object meets a set of criteria.
For example, the following method, which is of type Predicate<int>, could be used to select all integers greater than 5:
public bool MyPredicate(int x)
{
return x > 5;
}
I'm not sure how this translates into the more general case, but it's a start. For more info, click here.
ref type) and returns a bool, and assign it to a Predicate delegate. If it's possible, then the Predicate delegate doesn't make much sense.Also somewhat related, there are database-related predicates:
if (economy grows by 4%) then "sales forecast is valid" else "sales forecast is not valid". The "predicate" here is the test "economy grows by 4%", which would likely be implemented as a function which returns either true or false. Simplified, a predicate is the condition in an "if" statement (sometimes with side effects).