I'm trying to use ReplaceList to match subexpressions. I expected the command:

ReplaceList[{p, {p, p}}, {x_, y_} -> x y]

to output two results, equivalent to:
p {p, p}
and {p, p p}. That's because I expect the rule {x_,y_} to match both {p,{p,p}} and {p,p}. Only one result is output, equivalent to p {p,p}. I suppose that this is because ReplaceAll's behavior of replacing all subexpressions is special, whereas the default Replace that ReplaceList must use, only matches against the first level of the rule.

That's great, but how can I get the functionality I expect? I need this to apply to arbitrary levels of nesting. For example, I would expect it to turn {p, {p, {p, p}}} into

{ p {p, {p, p}}, {p, p {p, p}}, {p, {p, p p}} }

(I'm trying to reproduce the work on page 781 of the New Kind of Science book, in which subexpressions of Nand tautologies are matched/replaced according to the rules on page 775).

@rasher yes, that works for the case I gave, but it doesn't turn {p, {p, {p, p}}} into { {p {p, {p, p}}}, {p, p {p, p}}, {p, {p, p p}} }. Sorry for not being clear what I meant by "arbitrary levels of nesting".
–
NeuroFuzzyApr 7 '14 at 1:36

1

NKS book has code of its examples - it's at the end: For p.775 - p.1155 For p.781 - p.1157 BTW whatever essential info your add in the comments - you shoudl add it to your post too.
–
Vitaliy KaurovApr 7 '14 at 1:36

@VitaliyKaurov, p. 1157 states that "every possible transformation rule is applied wherever it can", but doesn't give an implementation. There's code on 1157, but this only generates the longest statement of its kind at every step. p. 1155 states the substitution rules I need to use, but I'm not sure how to apply one substitution rule in every possible way, at every level of the expression.
–
NeuroFuzzyApr 7 '14 at 1:47

1

@NeuroFuzzy: Ah, well, you could define transformations and use MapAll, or Fold over replacement rules. Not 100% certain ReplaceList alone will do what you want.
–
ciaoApr 7 '14 at 2:04

Mathematica is a registered trademark of Wolfram Research, Inc. While the mark is used herein with the limited permission of Wolfram Research, Stack Exchange and this site disclaim all affiliation therewith.