Home > Languages, Mathematica, Software > Defining new format wrappers in Mathematica

## Defining new format wrappers in Mathematica

June 3rd, 2009

One problem I had with the expression-to-C converter is that CForm converts negations of terms into a negation of parenthesized terms.

For example:

In:= -a b // CForm

Out//CForm= -(a*b)

That looks ugly. I tried to define a new format for Times[-1, v], but this requires unprotecting Times, and led to an infinite recursion when I tried to use it. I finally found a suggestion on how to define whole new format wrappers on the comp.soft-sys.math.mathematica list, in a message posted by Carl Woll (a Wolfram employee). This is how it works:

This assignment marks CCForm (my new formatter) as a format wrapper.

In:= Format[CCForm[expr_], CCForm] := expr

Note that the Out text shows the new formatter name.

In:= 2 + 3 // CCForm

Out//CCForm= CCForm

It’s showing the CCForm wrapper, though, which we don’t want to look at, so we define a top-level format for CCForm that replaces it with its argument.

In:= Format[CCForm[expr_]] := expr

Now, the output looks correct.

In:= 2 + 3 // CCForm

Out//CCForm= 5

The case I’m trying to fix isn’t helped by this. We need to make it produce the CForm.

In:= -a b // CCForm

Out//CCForm= -a b

In:= Format[CCForm[expr_]] := CForm[expr]

In:= -a b // CCForm

Out//CCForm= -(a*b)

We don’t want the result to be evaluated, so we add a HoldForm.

In:= Format[CCForm[expr_]] := HoldForm[CForm[expr]]

In:= -a b // CCForm

Out//CCForm= -(a*b)

Now it’s in CForm, but we still have the parenthesis. I don’t understand just why this works, but making CCForm HoldAll fixes the problem.

In:= SetAttributes[CCForm, HoldAll]

In:= -a b // CCForm

Out//CCForm= -a * b

This also prevents evaluation of the expression being formatted.

In:= 2 + 3 // CCForm

Out//CCForm= 2 + 3

Note that CForm does what I want if it’s made HoldAll.

In:= -a b // CForm

Out//CForm= -(a*b)

In:= SetAttributes[CForm, HoldAll]

In:= -a b // CForm

Out//CForm= -a*b

Although it’s not styled as nicely as the CCForm version, and could mess up existing code, so we won’t do that.

In:= ClearAttributes[CForm, HoldAll]

Now I can use CCForm instead of CForm for the output formatting of my converter, and have nicer-looking results.