Re: Flat, OneIdentity attributes

*To*: mathgroup at smc.vnet.net*Subject*: [mg21622] Re: Flat, OneIdentity attributes*From*: "Allan Hayes" <hay at haystack.demon.co.uk>*Date*: Tue, 18 Jan 2000 02:35:11 -0500 (EST)*References*: <85u3e7$d5j@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

Ted: below, I have added some comments to your text. I have tried to sort out at what is going with these and other aspects in the Mathematica in Education and Research, Volume 8, Numbers 3 and 4 (combined), in press. Allan --------------------- Allan Hayes Mathematica Training and Consulting Leicester UK www.haystack.demon.co.uk hay at haystack.demon.co.uk Voice: +44 (0)116 271 4198 Fax: +44 (0)870 164 0565 "Ersek, Ted R" <ErsekTR at navair.navy.mil> news:85u3e7$d5j at smc.vnet.net... For the most part I understand how Flat and OneIdentity are related and I demonstrate this using Version 4 in the examples below. In the first example (f) has the attributes Flat and OneIdentity. The pattern matcher treats f[a,2,3] as f[a,f[2,3]] then uses the replacement rule and {1,{2,3}} is returned. ClearAll[f]; Attributes[f] = {Flat, OneIdentity}; f[1, 2, 3] //. f[a_, b_] :> {a, b} {1, {2, 3}} (* Begin comment *) Follow the evaluation of ReplaceRepeated (//.) using ReplaceAll (/.): f[1, 2, 3] /. f[a_, b_] :> {a, b} {1, f[2, 3]} To get a match, the pattern matcher regarded f[1, 2, 3] as f[1,f [2, 3]] % /. f[a_, b_] :> {a, b} {1, {2, 3}} The pattern matcher used the rule literally. (* End comment *) --------------------------------------------------- In the next example the only attribute (f) has is Flat. In this case the pattern matcher treats f[1,2,3] as f[f[1],f[f[2],f[3]]] then uses the replacement rule and {f[1],{f[2],f[3]}} is returned. ClearAll[f]; Attributes[f] = {Flat}; f[1, 2, 3] //. f[a_, b_] :> {a, b} {f[1], {f[2], f[3]}} With OneIdentity the pattern matcher doesn't wrap (f) around a single argument when it tries different ways of nesting (f). (* Begin comment *) Follow the evaluation f[1, 2, 3] /. f[a_, b_] :> {a, b} {f[1], f[2, 3]} To get a match, the pattern matcher regarded f[1, 2, 3] as f[f[1],f [2, 3]] , *not as f[f[1],f[f[2],f[3]]]*. It looks like the steps may be: find a re-nesting f[1,f[2,3]] that matches and then, in the absence of OneIdentity, "level up" to f[f[1],f[2,3]] ", or it could be that only such level patterns are checked for matching. % /. f[a_, b_] :> {a, b} {f[1], {f[2], f[3]}} The pattern matcher regards f[2,3] as f[f[2],f[3]} Extra example: f[1] is regarded as f[f[1]] even when no re-nesting is needed to get a match.. f[1] /. f[a_] -> {a} {f[1]} (* End comment *) -------------------------------- In the next example (f) has the attributes Flat, OneIdentity and the rule is used. ClearAll[f] Attributes[f] = {Flat, OneIdentity}; f[2] /. f[n_Integer] :> n + 10 12 -------------------------------- For reasons I can't understand the rule isn't used in the next example. Can anyone explain why? ClearAll[f] Attributes[f] = {Flat}; f[2] /. f[n_Integer] :> n + 10 f[2] (* Begin comment *) In the example below shows that the rule is used, but that f[2] is regarded as f[f[2]]; this explains why in the original example the rule was not used :- f[2] does not match n_Integer. f[2] /. f[n_] :> { n} {f[2]} (* End comment *) --------------------------------------------- Regards, Ted Ersek For Mathematica tips, tricks see http://www.dot.net.au/~elisha/ersek/Tricks.html