1
$\begingroup$

I have a really, really complicated set of string simplification rules (only it doesn't actually work on strings, I use stuff like

F[x__,a,y__]:=F[x,b,y];
F[x__,b,b,y__]:=F[x,c,y];

to get the results on the fly - also I never know beforehand which rule is applicable in a concrete situation). As you can imagine, 100 of these rules have at least one hidden typo.
Is it possible to partially "Hold" the execution with something like this pseudocode happening

>exec F[a,a] 3 steps
F[b,a], rule 1 used
F[b,b], rule 1 used
F[c], rule 2 used
Done
>exec F[a,c] 1 steps
F[b,c], rule 1 used
Done

so I can then control by hand where a substition went haywire (which is luckily easy)? I.e. print out all intermediate results of the replacements?

$\endgroup$
2
  • $\begingroup$ shouldn't F[x__,a,y__] be F[x___,a,y___]? and similarly, F[x__,b,b,y__] should be F[x___,b,b,y___]? $\endgroup$
    – kglr
    Jul 3, 2019 at 14:38
  • $\begingroup$ Correct. I confused the two as in the actual case, rarely something happens at the boundary. $\endgroup$ Jul 4, 2019 at 10:43

1 Answer 1

0
$\begingroup$

Perhaps Trace gives what you need:

ClearAll[F]
F[x___, a, y___]:= F[x, b, y];
F[x___, b, b, y___]:= F[x, c, y];

Trace[F[a, a]]

{F[a, a], F[b, a], F[b, b], F[c]}

Trace[F[a, c]]

{F[a, c], F[b, c]}

$\endgroup$
1
  • 1
    $\begingroup$ I should have trusted my intuition which said "Surely, MATHEMATICA has something like a computation trace" and googled myself :-) Anyway, spot on. $\endgroup$ Jul 4, 2019 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.