Re: Defining a predicate
- To: mathgroup at yoda.physics.unc.edu
- Subject: Re: Defining a predicate
- From: rubin at msu.edu (Paul A. Rubin)
- Date: Fri, 24 Jun 94 15:29:51 EDT
>I want to define a predicate (TableauQ) to recognise a system (list) of
>equations, so that I set $PrePrint to apply TableForm to such a
>system. The following works:
>
> TableauQ[_] := False
> TableauQ[{_Equal..}] := True
> $PrePrint = If[TableauQ[#], TableForm[#],#]&;
>
>However, the logic of providing a blanket False (TableauQ[_] :=
>False), which is then overridden when appropriate pattern matches,
>bothers me. Is the explicit False necessary?
Seems so.
>On p. 227 of the good book, Wolfram writes:
>
> An important feature of Mma property-testing functions whose
>names end in Q is that they always return False if they cannot
>determine whether the expression has the given property.
>
>That behaviour also seems appropriate in my example. Is it possible
>to make user defined predicates exhibit the same behaviour?
If you mean establish a condition that all user-defined predicates with
names ending in 'Q' default to False if otherwise unresolved, it seems a bit
dangerous to want it. Is MyFunQ a predicate or a user-defined function?
Obviously, you can impose on yourself the condition that anything ending in
'Q' be a predicate, but what happens when you load an externally developed
package?
In any event, I'm pretty sure you cannot make that a standard in Mma. When
Mma goes to resolve a reference like MyFunQ[x], it looks for applicable
substitution rules, and specifically for rules associated with either
MyFunQ, x, or the Head of x. A condition like f_[_] := False if the name of
f ends in 'Q' would have no symbol with which to be associated.
BTW, I did a little experiment implementing a predicate three different
ways. My predicate checked for the Head "junk." The first way was yours:
f1Q[_] := False; f1Q[_junk] := True. The second, which bypasses the blanket
false but is logically equivalent, was: f1Q[x_] := If[ Head[x] == junk,
True, False ]. The third was like the first but with undelayed assignment:
f1Q[_] = False; f1Q[_junk] = True. The average of 10 applications to a list
of 200 critters, roughly half having head "junk," showed the second method
to take about 25% longer than the first, and the third to take about 60%
less time than the first. I presume that's because the delayed assignment
(:=) has to be evaluated each time the function is referenced. So you'll
pick up a little speed by dropping the colons.
>
>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>Michael Carter m.carter at econ.canterbury.ac.nz
>
>Department of Economics voice: + 64 3 364 2524
>University of Canterbury fax: + 64 3 364 2635
>Private Bag 4800
>Christchurch
>NEW ZEALAND
>
**************************************************************************
* Paul A. Rubin Phone: (517) 336-3509 *
* Department of Management Fax: (517) 336-1111 *
* Eli Broad Graduate School of Management Net: RUBIN at MSU.EDU *
* Michigan State University *
* East Lansing, MI 48824-1122 (USA) *
**************************************************************************
Mathematicians are like Frenchmen: whenever you say something to them,
they translate it into their own language, and at once it is something
entirely different. J. W. v. GOETHE