MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: pattern matching: rules that stop other rules?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg71603] Re: pattern matching: rules that stop other rules?
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Sat, 25 Nov 2006 05:36:38 -0500 (EST)
  • Organization: The Open University, Milton Keynes, UK
  • References: <ek63v6$913$1@smc.vnet.net>

croddie at princeton.edu wrote:
> Hello. I've been using Mathamatica for quite a while but without ever
> finding out how the language works fundamentally, which I'm trying to
> do now. I'd be grateful for some help in understanding patterns - it
> seems like a powerful idea to me. There is something I can't work out.
> 
> Define a function f [x_]:=0 say
> Now If [ p, 1, f [ 2] ] evaluates to itself. 

Correct. Why? Because p does not hold any logical value (True of False) 
yet. Therefore /If/ cannot determine which branch to compute and 
returned unevaluated.

f[x_] := 0
If[p, 1, f[2]]

--> If[p, 1, f[2]]

> So the rule in the
> definition is not applied to f [ 2 ].

Depending on the result of the evaluation of the predicate /p/, one but 
not the other argument will be evaluated.

Trace[If[p, 1, f[2]] /. p -> True]

--> {If[p, 1, f[2]] /.\[InvisibleSpace]p -> True, If[True, 1, f[2]], 1}

Trace[If[p, 1, f[2]] /. p -> False]

--> {If[p, 1, f[2]] /.\[InvisibleSpace]p -> False, If[False, 1, f[2]], 
f[2], 0}

> Replace If with some other undefined function, say qwerty, and you get
> qwerty [ p, 1, 0] not surprisingly. And If [ p, 1, f [ 2] ] /. f
> [x_]->0 returns If [ p, 1, 0 ].
> 
> Is there a rule associated with If that stops a rule (if that's the
> right expression) from being applied inside it? Can users write such
> rules?

Indeed, this is not a rule but the attribute /HoldRest/ that prevents 
the evaluation of any argument but the first.

Attributes[If]

--> {HoldRest, Protected}

So the standard behavior of the built-in function /If/ can be summarize 
as followed:

First, evaluate the first argument and do nothing with they other argument.

Second, if the result of the evaluation of the first augment cannot be 
interpreted as a logical value /True/ or /False/, return the expression 
unevaluated.

However, if the first augment evaluates to /True/, only then the 
evaluation of the second argument will occurs, and if the evaluation of 
the first argument returns /False/, in this case only the third -- and 
only the third -- argument will be evaluated.

Note that there exists a form of /If/ with a fourth arguments, argument 
that will be evaluated if the result of the evaluation of the first 
argument is neither /True/ nor /False/

If[p, 1, 2, f[2]]

--> 0

Trace[If[p, 1, 2, f[2]]]

--> {If[p,1,2,f[2]],f[2],0}


HTH,
Jean-Marc


  • Prev by Date: Re: dynamic Blocking and thread safeness (in general)
  • Next by Date: Re: a technique for options
  • Previous by thread: RE: pattern matching: rules that stop other rules?
  • Next by thread: Re: pattern matching: rules that stop other rules?