views:

784

answers:

10

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.

Cheers!

+16  A: 

A logical expression which evaluates to TRUE or FALSE, normally to direct the execution path in code.

Wahnfrieden
This answer is only partially true. A predicate is more specific that that. This definition of it is like saying the definition of "bibliophile" is someone who reads books. When the term bibliophile provides much more detail than that.
blesh
@blesh can you elaborate on what it is specifically?
Wahnfrieden
+14  A: 

A statement which is either true or false. In programming it is typically a function which return a boolean 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:

lessThanTen = function(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. Of course a boolean expression could be used as predicate in place of a function, e.g filter(true) will return the full list, filter(false) an empty list.

JacquesB
so basically it's a logical (boolean) expression?
Maciek
yeah, but since the predicate may rely on variables it it probably more natural to think of it as a function.
JacquesB
+3  A: 

A basic evaluation that results in a boolean(1) 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.

C. Ross
Misleading. Non-boolean values can still be returned and be evaluated in terms of true/false, though this is entirely language-dependent.
Wahnfrieden
I used boolean in the sense of boolean logic. http://en.wikipedia.org/wiki/Boolean_logic. Boolean logic is not language dependent.
C. Ross
In your first comment you said it was confusing because non-booleans can be true/false... now you say it's misleading because it non-boolean inputs can result in boolean outputs. In a theoretical sense boolean means true or false, and I said it results in one of those. How is that confusing?
C. Ross
A: 

Also somewhat related, there are database-related predicates:

http://www.tizag.com/sqlTutorial/sqlpredicates.php

Jon
+1  A: 

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.

ph0enix
He *did* tag this [General]...
Wahnfrieden
C# is fine as well.
Maciek
I don't know whether it's possible to create a function that alters an object (instance of a `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.
Eduardo León
+1  A: 

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?
BCS
More specifically, it's a yes or no question.
rlbond
This is stackoverflow, not an English discussion forum
Wahnfrieden
I’m not very firm in grammar but still, my impression until now was that a predicate is the *answer* to the above questions, not the question itself (so “This **is red**.”, “This **is a dog**.”, “This **is owned by them**.”) Can you clarify? Is both correct?
Konrad Rudolph
I know from my logic class that in formal logic, predicates are the function analog. So in that context it is the question, not the answer. I'm not so sure in normal English.
BCS
A: 

A function that returns a boolean. Predicates are used a lot in functional and OO programming to select subsets of values from data structures, especially lists and other collections. You'll find plenty of examples in the standard libraries for Haskell and Smalltalk.

Norman Ramsey
+7  A: 

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?

blesh
+1... All of this is easy in C++. A predicate is a function whose parameters are all either by value or `const` references, and whose return type is a `bool`.
Eduardo León
Eduardo: It's not necessarily bool, thanks to coercion
Wahnfrieden
My answer already covered this nuance by using the word "expression." Also, a method definition is not a predicate - it's not even an expression. Your answer is rather confused and misleading.
Wahnfrieden
I'm sorry I confused you.
blesh
A: 

It is probably useful to consider the grammatical meaning of the concept to extrapolate the programming concept.

From wikipedia:

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

Gordon Potter