12

At mathkb.com, I found a interesting post "Another review of Mathematica's debugger" (by berniethejet) talking about debugging in wolfram workbench.

http://www.mathkb.com/Uwe/Threads/List.aspx/mathematica/20986

I think this is a good question worth discussing and I would like hear some experiences of using workbench, even though I've never touched workbench.

  1. Is workbench a real debugger but a watcher? what's its advantage over mathematica?
  2. How do you debug when you writting big or small codes? mabye workbench is for debugging small codes and mma debugger for large codes ?
  3. Any suggestion on debugging for both light and heavy mathemata users?
3
  • This is quite possibly off-topic for StackOverflow. I am not active on programmers.stackexchange.com but I believe it would be better asked there.
    – Mr.Wizard
    May 29, 2011 at 10:55
  • Thanks, Wizard. I serached "mathematica" in programmers.stackexchange.com, only several very helpful results are hit.
    – FreshApple
    May 29, 2011 at 11:31
  • 1
    It may be a bit of the "chicken or the egg" problem, but I hope that this question would draw attention, especially if it is migrated from here. If you feel I am correct, you can click "flag" below your post, select "Other" and ask a moderator to migrate this to Programmers.
    – Mr.Wizard
    May 29, 2011 at 11:51

4 Answers 4

21

Debuggers are generally more useful when you program in the stateful style (variables, assignments, etc) - at least that has been my experience. For idiomatic Mathematica programming (functional/rule-based), some versions of Print statements are at least as effective. You may look at this post for some variants of debug print utility. I will throw in my version taken from this Mathgroup post.

SetAttributes[ShowIt, HoldAll];
ShowIt[code_] :=
  Module[{y},
    Print[ToString[Unevaluated[code]], " = ", y = code];
    y]; 

The idea is that you can insert such function call into a "pipe" of function calls - it prints the value but then passes it to the next (surrounding) function. As a simple example:

In[29]:= Map[#^2&,ShowIt@Select[Range[10],EvenQ]]
During evaluation of In[29]:= Select[Range[10], EvenQ] = {2,4,6,8,10}

Out[29]= {4,16,36,64,100}

This should work fine in most cases (except possibly those where the surrounding function holds its arguments and acts on them non-trivially). One of the reasons that this approach is very effective in Mathematica is that functional programming leads to programs in which (almost) every piece makes sense by itself - since the result of one function is typically passed directly to the enclosing function.

That said, you can certainly use the debugger, both within interactive session and in the WorkBench, using "Debug As Mathematica" regime. While I use WorkBench a lot myself, I never found this necessary, but YMMV.

Another great facility that helps a lot is a built-in Trace command. I recommend reading the documentation on it - it has a number of advanced options and can be customized to help a great deal. I will give one simple but non-trivial example: tracing the execution of the mergesort algorithm, with the following (simplistic) implementation:

Clear[merge];
merge[{}, {x__}] := {x};
merge[{x__}, {}] := {x}
merge[{x__?NumericQ}, {y__?NumericQ}] /; First[{x}] <= First[{y}] := 
  Flatten[{First[{x}], merge[Rest[{x}], {y}]}];
merge[{x__?NumericQ}, {y__?NumericQ}] := merge[{y}, {x}];

Clear[mergesort];
mergesort[x : {} | {_}] := x;
mergesort[x : {__?NumericQ}] := 
 With[{splitlen = IntegerPart[Length[x]/2]}, 
   merge[mergesort[Take[x, splitlen]], mergesort[Drop[x, splitlen]]]]

We will take a very small input list, just to reduce the length of the output:

In[41]:= testlst = RandomInteger[10, 5]

Out[41]= {0, 6, 9, 8, 8}

You could just use Trace[mergesort[testlst]];, but the output is not very easy to read, since it contains all the steps. By using

In[42]:= Trace[mergesort[testlst],_mergesort]

Out[42]= {mergesort[{0,6,9,8,8}],{mergesort[{0,6}],{mergesort[{0}]},
{mergesort[{6}]}},{mergesort[{9,8,8}],{mergesort[{9}]},{mergesort[{8,8}],
{mergesort[{8}]},{mergesort[{8}]}}}}

You get a very clear picture of recursive function calls. You can go deeper and trace the dynamics of merge function. For that, you have to process the result of Trace (which is also a Mathematica expression!):

In[43]:= 
Cases[Trace[mergesort[testlst],_merge],merge[x__List]/;FreeQ[{x},mergesort]:> 
 HoldForm[merge[x]],Infinity]

Out[43]= {merge[{0},{6}],merge[{},{6}],merge[{8},{8}],merge[{},{8}],
merge[{9},{8,8}],merge[{8,8},{9}],merge[{8},{9}],merge[{},{9}],merge[{0,6},
{8,8,9}],merge[{6},{8,8,9}],merge[{},{8,8,9}]}

This last example illustrates that, even when it is hard to configure Trace directly to filter out unwanted execution steps, one can simply post-process the results of Trace using standard means that Mathematica provides for expression destructuring (such as Cases).

Let me also mention that an expert Mathematica user and consultant David Bailey wrote a package DebugTrace, which is supposed to be an alternative debugger. I did not have a chance yet to try it, but I am sure it is worth the try.

Finally, while this is not directly related to debugging, WorkBench has an integrated unit testing framework MUnit, which I found very useful. It is similar in spirit to well-known unit-testing frameworks in other languages, such as JUnit for Java. For large-scale development, this can be a real help.

Regarding the uses of WorkBench, I'd say that it really pays off to use it for anything except the smallest projects (or even for them). It is based on Eclipse, and you get the same nice things, such as the editor with code highlighting, "go to function definition" capability, navigation, search, CVS/SVN integration, etc. At the same time, you don't lose almost anything in terms of interactivity - you can still develop the new functionality in the interactive Mathematica session linked to WorkBench when working in the "Run as Mathematica" regime. For larger projects involving many packages, I just don't see any reason not to use it.

8
  • Thanks for sharing your valuable experience! Shifrin. :)
    – FreshApple
    May 29, 2011 at 14:38
  • Thanks Leonid, nice post. For Workbench, I found it awkward to use, as it is not integrated in Mathematica notebook itself, and one must launch another, separate program to debug things. I am used to Matlab's debugger, which I find much easier and more intuitive to use, since it is build right into the editor itself, I can turn it on and off at well much easier. I think Mathematica debugger should be build as part of the notebook editor/interface as well. I have workbench 2.0 also, and tried it few times, and stopped using it. Too confusing to use and set up each time I want to check something
    – Nasser
    May 29, 2011 at 17:50
  • @Nasser I don't have much experience with WorkBench debugger, since I usually debug by other means (which I mentioned). Regarding WorkBench itself, I use it on a daily basis and find it an indispensable tool. I often work with many packages, and I simply don't know what I would do without WB. I am somewhat biased since I used Eclipse a lot on my Java job in the past, but, in terms of IDE capabilities, I think there is simply no comparison between Matlab's homegrown IDE and WB that is based on industrial strength solution (Eclipse) widely used in production for huge projects. May 29, 2011 at 18:02
  • @Leonid, I am confused, I thought WB is just a debugger, I never thought to use it as a replacement to the the notebook interface itself? This is what I normally do: start Mathematica, open a notebook and work there. When I wanted to debug a function, move it to one cell on its own, go start WB, navigate to open the notebook itself that is already open, click few buttons, copy and load the function code into WB, and few other things I forgot since last I used WB was a year ago. If I can actually use WB to replace notebook then I would get used to it, Have to try that soon.
    – Nasser
    May 29, 2011 at 21:31
  • @Leonid: Wish I had upvoted you 15 mins earlier, then you would've gotten a Mortarboard badge! Anyway, congrats on 5k!
    – user564376
    May 30, 2011 at 0:15
4

Using the debugger in Wolfram Workbench makes debugging simple and effective. The reason I started to use Workbench was the debugger. The Workbench also supports MUnit the Mathematica variant of JUnit. - "Test first, then code."

The debugger in Workbench supports everything that I expected from a debugger. I have used the Java debuggers in Eclipse and NetBeans.

At least give the debugger a try, so that you can compare. There is a tutorial on the Workbench Docs site.

1
  • Actually our research group plan to write a small general PDE based multiphysics package which may need constantly bug fixing. I guess workbench is more suitale for this project. Thanks, ndroock1. :)
    – FreshApple
    May 30, 2011 at 2:16
3

Here are some variations of ShowIt described by Leonid. Defining them in the System context allows to use them easily in packages.

SetAttributes[System`ShowIt, HoldAll];
System`ShowIt[code__] := System`ShowIt[{code}];
System`ShowIt[code_] :=
   With[{y = code},
      Print[Defer[code = y]];
      y
   ]; 

SetAttributes[System`PrintIt, {HoldAll,Listable}];
System`PrintIt[expr__]:=System`PrintIt[{expr}];
System`PrintIt[expr_] := System`ShowIt[expr];

Example:

ShowIt[{x=2,x=3}]
PrintIt[{x=2,x=3}]

The output of these functions can be easily reused in the frontend by changing its style to "Input".

2

I've had limited success with the debugger, mostly because I never took the time to properly learn it. I do use one technique often though. Instead of resorting to print statements, I make expressions under my manipulate (or whatever), of the form Dynamic[var]. Your can easliy monntor any file global variable in real time this way without generating huge output. To see manipulate variables, use LocalizeVariables->False and do the same thing. Outside of the manipulate context, the variables are visible, but not dynamic; their monitoring is thus the same.

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.